我有一系列单词$duplicate_words
和一个字符串$contents
。我想搜索字符串中的单词并在匹配时突出显示它们。
我可以突出显示单个重复单词,但我不想突出显示单个单词 - 我只想在两个或更多单词匹配时突出显示。我一直试图找到解决方案一个多星期没有成功。有什么想法吗?
答案 0 :(得分:0)
试试这个:
$no_of_matches = preg_match_all('/'.$duplicate_word.'/', $string, $matches);
if($no_of_matches > 1){
$string = str_replace($duplicate_word, '<span class="highlight">' . $duplicate_word . '</span>', $string);
}