Ignore duplicates in regex pattern
Posted
by
gAMBOOKa
on Stack Overflow
See other posts from Stack Overflow
or by gAMBOOKa
Published on 2010-12-22T09:28:04Z
Indexed on
2010/12/22
9:54 UTC
Read the original article
Hit count: 354
I have a regex pattern that searches for words in a text file. How do I ignore duplicates?
For instance, take a look at this code
$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] );
If I have more than one say lorem in the article, the output will be something like this
5 matches found!
Matched words: daboom,lorem,lorem,lorem,lorem
I want the pattern to only find the first occurrence, and ignore the rest, so the output should be:
2 matches found!
Matched words: daboom,lorem
© Stack Overflow or respective owner