Search code examples
phpstringarrayscontains

String contains any items in an array (case insensitive)


How can i check if a $string contains any of the items expressed in an array?

$string = 'My nAmE is Tom.';
$array = array("name","tom");
if(contains($string,$array))
{
// do something to say it contains
}

Any ideas?


Solution

  • is that what you wanted? i hope that code is compiling :)

    $string = 'My nAmE is Tom.';
    $array = array("name","tom");
    if(0 < count(array_intersect(array_map('strtolower', explode(' ', $string)), $array)))
    {
      //do sth
    }