String contains any items in an array (case insensitive)
I don't think there is a built-in function that will handle what you want. You could easily write a contains()
function however:
function contains($str, array $arr)
{
foreach($arr as $a) {
if (stripos($str,$a) !== false) return true;
}
return false;
}
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
}
Using the accepted answer:
$string = 'My nAmE is Tom.';
$array = array("name","tom");
if(0 < count(array_intersect(array_map('strtolower', explode(' ', $string)), $array)))
{
//do sth
}
Just a side note that the if statement could be changed to:
if(0 < count(array_intersect(explode(' ', strtolower($string)), $array)))
since it's not really necessary to use array_map to apply strtolower
to each element. instead apply it to the initial string.