Home > php教程 > php手册 > body text

PHP给文字内容中的关键字进行套红处理,

WBOY
Release: 2016-06-13 08:42:08
Original
1041 people have browsed it

PHP给文字内容中的关键字进行套红处理,

有些时候我们在显示一篇文章的时候,可能需要把某些关键字进行套红,高亮显示,以便我们能快速的查找和定位这些关键字,下面让我们看看具体实现的代码。

/**
  * 关键字套红方法
  * 
  * @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 );
Copy after login

您可能感兴趣的文章:

  • ThinkPHP做文字水印时提示call an undefined function exif_imagetype()解决方法
  • PHP中创建图像并绘制文字的例子
  • php支持中文字符串分割的函数
  • PHP实现过滤掉非汉字字符只保留中文字符
  • php给图片加文字水印
  • php自动识别文字编码并转换为目标编码的方法
  • php面向对象与面向过程两种方法给图片添加文字水印
  • php给图片添加文字水印方法汇总
  • php图片添加文字水印实现代码
  • 编写PHP程序检查字符串中的中文字符个数的实例分享
Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!