PHP search and highlight keywords in strings

WBOY
Release: 2016-07-25 08:42:46
Original
1092 people have browsed it
  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


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!