PHP keyword red marking processing class

*文
Release: 2023-03-18 20:40:02
Original
3833 people have browsed it

Sometimes when we display an article, we may need to highlight certain keywords in red so that we can quickly find and locate these keywords. This article introduces a function in PHP that can mark the keywords of the content in red, highlight the keywords, and share it with everyone. I hope it will be helpful to everyone when dealing with 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(&#39;item_title&#39;,&#39;keywords&#39;, &#39;photo_title&#39;, &#39;photo_site&#39;,&#39;content&#39;,)))
    {
     $info = strip_tags($info);
     $info = str_replace(&#39; &#39;, &#39;&#39;, $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=&#39;red&#39;>{$search_str}</font>", $info);
 
      if($info_arr[&#39;match_key&#39;]==&#39;&#39;)
      $info_arr[&#39;match_key&#39;] = $key;
     }
     else
     {
      $info = csubstr($info,300);
     }
    }
    $info_arr[$key] = $info;
   }
  }
  return $info_arr;
 }
 
$str = &#39;woloveu小君&#39;;
$info_arr = array(&#39;photo_title&#39; => &#39;womejiojd我们都围殴防静低屁啊小君鸡冻啊泡脚盆大&#39;);
$search_arr = array(&#39;小君&#39;);
$ret = set_arr_keyword_red($info_arr, $search_arr);
dump($ret );
Copy after login

Related recommendations:

php Keyword Baidu Ranking Batch Check_PHP Tutorial

php Detailed explanation of string regular replacement function preg_replace

php String replacement method

The above is the detailed content of PHP keyword red marking processing class. For more information, please follow other related articles on the PHP Chinese website!

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 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!