PHP加粗搜索结果中关键字
程序员文章站
2022-04-13 14:09:30
...
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);