Moj123
Moj123

Reputation: 55

Compare two strings and find multiple matches

I have two strings I want to compare and return the number of occurrences found.

For example.

$string = "123,456,789";

$find = "123,657,456";

With the right function this should return "2"

I've tried

echo substr_count($string,$find);

Which returns "0", but if $find = "123" then it will return "1"

I know I could do this if I used a loop and checked each 3 digits seperately but I want to avoid using a loop to allow readability (I'm already using a lot of loops).

If this at all possible with any existing functions or one liners? Or will I have to create my own helper function or loop?

Upvotes: 1

Views: 57

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

Easy using arrays:

echo count(array_intersect(explode(',', $string), explode(',', $find)));

Upvotes: 2

Related Questions