我使用以下内容检查表单中的数据是否包含某些单词,问题是,当它检查时我发现“找不到匹配”或“找不到匹配”一遍又一遍一百万次。如何为发布的整个句子分配真值或假值?我想添加类似的东西....如果真的转储这个帖子,如果false处理它并把它放在数据库中。
<?php
$message = $_POST['message'];
echo $message;
$targets = array('viagra', 'prescription', 'drugs', 'zyban', 'zithromax', 'voltaren', 'cilias', 'FDA');
foreach($targets as $t)
{
if (preg_match("/\b" . $t . "\b/i", $message)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
}
?>
答案 0 :(得分:3)
function containsSpam($message) {
$targets = array('viagra', 'prescription', 'drugs', 'zyban', 'zithromax', 'voltaren', 'cilias', 'FDA');
foreach($targets as $t)
{
if (preg_match("/\b" . $t . "\b/i", $message)) {
//A match was found.
return true;
}
}
return false;
}
答案 1 :(得分:0)
如果我做得对,你需要像这样设置一个停止值
<?php
function containsSpam($message) {
$targets = array('viagra', 'prescription', 'drugs', 'zyban', 'zithromax', 'voltaren', 'cilias', 'FDA');
$stop = false;
foreach($targets as $t)
{
if (preg_match("/\b" . $t . "\b/i", $message)) {
//A match was found.
$stop = true;
break;
}
}
//And you can then do something like this
if ($check) {
//Insert it
} else {
//Dump it
}
}
?>