Ignore duplicates in regex pattern
- by gAMBOOKa
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