Reputation: 431
I want to prevent the contact form from submission if the message field contains certain words. I used one, two and three as an example:
//Prevent the form from submission if it contains one, two, or three
$needle = ['one', 'two', 'three'];
if (stripos($message, $needle) !== false) {
echo "$message contains $needle";
}
This did not work for me. However, I tested this with one word only and it worked:
if (stripos($message, 'one') !== false) {
echo 'invalid message format';
}
How can I check on multiple words in a message in PHP if the above array is not working?
Upvotes: 1
Views: 291
Reputation: 1803
You need to use for loop.
$needle_arr = ['one', 'two', 'three'];
$included = [];
foreach($needle_arr as $needle)
if (stripos($message, $needle) !== false) {
$included []= $needle;
}
if(count($included) > 0)
echo "$message contains ".implode(", ", $included);
Upvotes: 1
Reputation: 782158
You need to loop over the array.
$word_found = false;
foreach ($needles as $word) {
if (stripos($message, $word) !== false) {
$word_found = $word;
break;
}
}
if ($word_found) {
echo "$message contains $word_found";
}
Upvotes: 0