如果消息字段包含某些单词(PHP),如何阻止表单提交?
How to block form submissions if the message field contains certain words (PHP)?
如果消息字段包含某些词,我想阻止提交联系表。我用 one, two 和 three 作为例子:
//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";
}
这对我不起作用。然而,我只用一个词测试了它并且它起作用了:
if (stripos($message, 'one') !== false) {
echo 'invalid message format';
}
如果上述数组不起作用,我如何检查 PHP 中消息中的多个单词?
你需要使用for循环。
$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);
您需要遍历数组。
$word_found = false;
foreach ($needles as $word) {
if (stripos($message, $word) !== false) {
$word_found = $word;
break;
}
}
if ($word_found) {
echo "$message contains $word_found";
}
如果消息字段包含某些词,我想阻止提交联系表。我用 one, two 和 three 作为例子:
//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";
}
这对我不起作用。然而,我只用一个词测试了它并且它起作用了:
if (stripos($message, 'one') !== false) {
echo 'invalid message format';
}
如果上述数组不起作用,我如何检查 PHP 中消息中的多个单词?
你需要使用for循环。
$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);
您需要遍历数组。
$word_found = false;
foreach ($needles as $word) {
if (stripos($message, $word) !== false) {
$word_found = $word;
break;
}
}
if ($word_found) {
echo "$message contains $word_found";
}