You can use a loop to iterate over the array of forbidden words and check if any of them exist:
$string = "a very long text ....";
$forbidden_words = array("word1", "word2", "word3");
foreach ($forbidden_words as $word) {
if (str_contains($string, $word)) {
echo "The string contains the word: $word";
}
}
Alternative to check if any of them appear in the string (using strpos
) :
$string = "a very long text ....";
$forbidden_words = array("word1", "word2", "word3");
$contains_forbidden_word = false;
foreach ($forbidden_words as $word) {
if (strpos($string, $word) !== false) {
$contains_forbidden_word = true;
break;
}
}
if (!$contains_forbidden_word) {
// The string does not contain any of the forbidden words
}