Check if a String Contains Multiple Words in PHP

Check if a string contains multiple words:

function str_contains_multiple_words($string, $array_of_words)
{
   if (!$array_of_words) return false;
 
   $i = 0;
 
   foreach ($array_of_words as $words)
   {
        if (strpos($string, $words) !== FALSE) $i++;
   }
 
   return ($i == count($array_of_words)) ? true : false;
}

Example usage:

if(str_contains_multiple_words("this is a string", array("is", "string")))
{
    echo 'Words "is" and "string" found!';
}
    echo 'Words "is" and "string" not found!';
}

More Related Posts