我有一个正则表达式模式,可以在文本文件中搜索单词。如何忽略重复的单词?
例如,看看这段代码:
$pattern = '/(lorem|ipsum|daboom|pahwal|ababaga)/i';
$num_found = preg_match_all( $pattern, $string, $matches );
echo "$num_found match(es) found!";
echo "Matched words: " . implode( ',', $matches[0] );
如果文章中有多个lorem,输出将会是这样的:
5 matches found!
Matched words: daboom,lorem,lorem,lorem,lorem
我希望这个模式只能找到第一次出现的内容,而忽略其余的内容,因此输出应该是:
2 matches found!
Matched words: daboom,lorem