当前位置:Gxlcms > PHP教程 > PHP搜索和高亮字符串中的关键字

PHP搜索和高亮字符串中的关键字

时间:2021-07-01 10:21:17 帮助过:6人阅读

  1. function highlighter_text($text, $words)
  2. {
  3. $split_words = explode( " " , $words );
  4. foreach($split_words as $word)
  5. {
  6. $color = "#4285F4";
  7. $text = preg_replace("|($word)|Ui" ,
  8. "$1" , $text );
  9. }
  10. return $text;
  11. }

用法:
  1. $string = "I like chocolates and I like apples";
  2. $words = "apple";
  3. echo highlighter_text($string ,$words);
  4. ?>

PHP

人气教程排行