Unfortunately, you cannot directly pass functions Arrayto strpos / strstr functions.
According to the comments of PHP.net, for this you need to use (or create your own, if you want) function as follows:
function strstr_array($haystack, $needle)
{
if ( !is_array( $haystack ) ) {
return false;
}
foreach ( $haystack as $element ) {
if ( stristr( $element, $needle ) ) {
return $element;
}
}
}
Then just change strposto a new function - strstr_array:
$bad_words = array("yo","hi");
$sentence = "yo";
if (strstr_array($bad_words,$sentence)==false) {
echo "success";
}
source
share