3

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

1 Answer 1

6

Do an array_unique on $matches[0]. And maybe an array_map with strtolower if you want the unique to be case insensitive.

$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 );
Sign up to request clarification or add additional context in comments.

1 Comment

slaps forehead Why didn't I think of that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.