Home  >  Article  >  Backend Development  >  PHP redens keywords in text content, _PHP tutorial

PHP redens keywords in text content, _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:54:32791browse

PHP redens the keywords in the text content.

Sometimes when we display an article, we may need to highlight certain keywords in red. Display so that we can quickly find and locate these keywords. Let's take a look at the specific implementation code.

/**
  * 关键字套红方法
  * 
  * @access public
  * @param array $options 参数数组
  *  <li> $info_arr  array  内容</li>
  *  <li> $search_arr array  关键词数组 </li>
  * @return int or array
  */
  function set_arr_keyword_red($info_arr, $search_arr)
 {
  foreach ($search_arr as $search_str)
  {
   foreach ($info_arr as $key => $info)
   {
    if(in_array($key,array('item_title','keywords', 'photo_title', 'photo_site','content',)))
    {
     $info = strip_tags($info);
     $info = str_replace(' ', '', $info);
     $q_str_pos = stripos($info, $search_str);
     if (false!==$q_str_pos)
     {
      $info = csubstr($info, $q_str_pos+150);
      $temp = csubstr($info,$q_str_pos-150);
      $info = substr($info, strlen($temp), 300);
      $info = preg_replace("/{$search_str}/i", "<font color='red'>{$search_str}</font>", $info);
 
      if($info_arr['match_key']=='')
      $info_arr['match_key'] = $key;
     }
     else
     {
      $info = csubstr($info,300);
     }
    }
    $info_arr[$key] = $info;
   }
  }
  return $info_arr;
 }
 
$str = 'woloveu小君';
$info_arr = array('photo_title' => 'womejiojd我们都围殴防静低屁啊小君鸡冻啊泡脚盆大');
$search_arr = array('小君');
$ret = set_arr_keyword_red($info_arr, $search_arr);
dump($ret );

Articles you may be interested in:

  • ThinkPHP prompts call an undefined function exif_imagetype() solution when making text watermarks
  • Examples of creating images and drawing text in PHP
  • php supports Chinese string splitting function
  • PHP implements filtering out non-Chinese characters and only retains Chinese characters
  • php adds text watermark to pictures
  • php automatically Methods to identify text encoding and convert to target encoding
  • PHP object-oriented and process-oriented methods to add text watermarks to pictures
  • A summary of methods to add text watermarks to pictures in PHP
  • Implementation code for adding text watermarks to php images
  • Sharing examples of writing a PHP program to check the number of Chinese characters in a string

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1119967.htmlTechArticlePHP redens the keywords in the text content. Sometimes when we display an article, it may Some keywords need to be highlighted in red so that we can quickly...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn