正则表达式是必经之路!
function highlight($text, $words) { preg_match_all('~w+~', $words, $m); if(!$m) return $text; $re = '~\b(' . implode('|', $m[0]) . ')\b~'; return preg_replace($re, '<b>$re = '~\b(' . implode('|', $m[0]) . ')\b~i';</b>', $text);}$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmodtempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodoconsequat.';$words = 'ipsum labore';print highlight($text, $words);
要以不区分大小写的方式进行匹配,请在正则表达式中添加“ i”
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)