eloleon
eloleon

Reputation: 1254

PHP: compare two strings for existence of 2 same words in both of them

I need to create a function that will return true or false based on comparison results.

I have two strings:

$one = 'sample product - White - Large'; // White is separate key, and large is separate too
$two = 'White Large';

Do you know any quick way for doing that task? Exploding two strings might not be safe as some strings instead of White will have: Blue - Black - Large. So I'm looking for existence of Blue - Black.

Another example of my data is:

$one = 'sample product - White - Blue - Large';  // White - Blue is separate key, Large is separate too.

If someone have any ideas on how to do it, I would really appreciate it.

Thanks, eloleon

Upvotes: 3

Views: 2800

Answers (1)

arkascha
arkascha

Reputation: 42885

I'd go for a strategy like that:

  1. explode the strings into arrays by whitespaces (using regexes)
  2. use array_intersect() to find matches

Upvotes: 7

Related Questions