Home  >  Article  >  Backend Development  >  PHP search and highlight keywords in strings

PHP search and highlight keywords in strings

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


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
Previous article:PHP decompress ZIP fileNext article:PHP decompress ZIP file