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

时间:2009-12-10 05:23:06

标签: php

我有一系列单词$duplicate_words和一个字符串$contents。我想搜索字符串中的单词并在匹配时突出显示它们。

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

1 个答案:

答案 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);
}