使用php比较和突出显示文本中的短语

使用php比较和突出显示文本中的短语,php,Php,我有一个单词数组$duplicate\u words和一个字符串$contents。我想搜索字符串中的单词,并在匹配时突出显示它们 我可以突出显示单个重复的单词,但我不想突出显示单个单词-我只想在两个或多个单词匹配时突出显示。一个多星期以来,我一直在努力寻找解决方案,但没有成功。有什么想法吗?试试这个: $no_of_matches = preg_match_all('/'.$duplicate_word.'/', $string, $matches); if($no_of_matches &

我有一个单词数组
$duplicate\u words
和一个字符串
$contents
。我想搜索字符串中的单词,并在匹配时突出显示它们

我可以突出显示单个重复的单词,但我不想突出显示单个单词-我只想在两个或多个单词匹配时突出显示。一个多星期以来,我一直在努力寻找解决方案,但没有成功。有什么想法吗?

试试这个:

$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);
}
$no_of_matches=preg_match_all('/'.$duplicate_word'/',$string,$matches);
如果($no_of_matches>1){
$string=str_replace($duplicate_word,“.$duplicate_word.”,$string);
}

您想在IDE中查找并突出显示字符串,还是想创建一个可以这样做的程序?我想为此制作一个程序。