PHP搜索和高亮字符串中的关键字
时间:2021-07-01 10:21:17
帮助过:6人阅读
- function highlighter_text($text, $words)
- {
- $split_words = explode( " " , $words );
- foreach($split_words as $word)
- {
- $color = "#4285F4";
- $text = preg_replace("|($word)|Ui" ,
- "$1" , $text );
- }
- return $text;
- }
用法: - $string = "I like chocolates and I like apples";
- $words = "apple";
- echo highlighter_text($string ,$words);
- ?>
|
PHP