欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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);