Rumah >pembangunan bahagian belakang >tutorial php >PHP加粗搜索结果中关键字
<p>function highlighter_text($text, $words){</p><p> $split_words = explode( " " , $words );</p><p> foreach($split_words as $word) {</p><p> $color = "#4285F4";</p><p> $text = preg_replace("|($word)|Ui" , "<b>$1</b>" , $text );</p><p> }</p><p> return $text;</p><p>}</p><p>$string = "I like chocolates and I like apples";$words = "apple";</p><p>echo highlighter_text($string ,$words);</p>