I have a regular expression pattern that searches for words in a text file. How to ignore duplicates?
For example, 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 word lorem in an article, the output will be something like this
5 matches found!
Matched words: daboom,lorem,lorem,lorem,lorem
I want the template to detect only the first occurrence and ignore the rest, so the output should be:
2 matches found!
Matched words: daboom,lorem
source
share