忽略正则表达式模式中的重复内容

3

我有一个正则表达式模式,可以在文本文件中搜索单词。如何忽略重复的单词?

例如,看看这段代码:

$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
1个回答

6

$matches[0]执行array_unique操作。如果要使唯一性不区分大小写,可以使用array_mapstrtolower

$pattern = '/(lorem|ipsum|daboom|pahwal|ababaga)/i';
preg_match_all( $pattern, $string, $matches );
$matches = $matches[0]?array_unique(array_map('strtolower', $matches[0])):array();

echo count($matches)." match(es) found!";
echo "Matched words: " . implode( ',', $matches );

"拍了拍额头",为什么我没想到呢? - HyderA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接