in_string like in_array

Is there an in_string function like in_array?

I would like to use an array for my $needle and a string for my $haystack.

So if my $needle was array(‘Mary’, ‘Frank’, ‘Joe’) and my $haystack was ‘Tom went to Mary’s party’, true would be returned.

I have my own quick and dirty way of doing it but I have the feeling there is a more elegant solution.


if (preg_match('/Mary|Frank|Joe/', $haystack) == 1)
{
  ...
}

Thank you! Is there an upper limit to the number of elements you can have in the $needle?

Nope :smiley:

Although the more items you use, the slower the script would be - however it won’t be noticably slow unless the list is huge.

My stab at it:

function in_string($needle, $haystack) {
  if is_array($needle) {
    foreach ($needle as $n) {
      if (strpos($haystack, $n) !== false) return true;
    }
  }
  else return strpos($needle, $haystack) !== false;
}