Home > Article > Backend Development > php正则加亮关键字函数方法
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] = '<font style="background-color:'.$color.';">\\1</font>'; } } else { $pattern = "/\b($words)\b/is"; $replace = '<font style="background-color:'.$color.';">\\1</font>'; } return preg_replace($pattern,$replace,$str); }