高亮显示搜索结果代码
function highlight_words($str, $words, $color = '#FFFF00') {
if(is_array($words)) {
foreach($words as $k => $word) {
$pattern[$k] = "/b($word)b/is";
$replace[$k] = '\1';
}
}
else {
$pattern = "/b($words)b/is";
$replace = '\1';
}
return preg_replace($pattern,$replace,$str);
}
$str="sdfasdfasdfas AS asds";
$word="as";
echo highlight_words($str,$word);
?>