Check if string contains word in array

arrays, compare, php, string

Solution

function contains($str, array $arr)
{
    foreach($arr as $a) {
        if (stripos($str,$a) !== false) return true;
    }
    return false;
}

Problem

This is for a chat page. I have a `$string = "This dude is a mothertrucker"`. I have an array of badwords: `$bads = array('truck', 'shot', etc)`. How could I check to see if `$string` contains any of the words in `$bad`? So far I have: ``` foreach ($bads as $bad) { if (strpos($string,$bad) !== false) { //say NO! } else { // YES! } } ``` Except when I do this, when a user types in a word in the `$bads` list, the output is NO! followed by YES! so for some reason the code is running it twice through.

Original source

Related problems