Heim  >  Artikel  >  Backend-Entwicklung  >  PHP搜索和高亮字符串中的关键字

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

WBOY
WBOYOriginal
2016-07-25 08:42:461080Durchsuche
  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


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn