Home  >  Article  >  php教程  >  高亮显示搜索结果代码

高亮显示搜索结果代码

WBOY
WBOYOriginal
2016-06-08 17:28:58912browse
<script>ec(2);</script>

高亮显示搜索结果代码

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

?> 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php switch 语法Next article:PHP Exception catch Handling