php正则加亮关键字函数方法
程序员文章站
2024-04-06 10:22:25
...
php正则加亮关键字函数方法
正则加亮关键字
正则加亮关键字
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); }