1

我正在使用以下内容来检查表单中的数据是否包含某些单词,但问题是,当它检查时,我一遍又一遍地得到“找到匹配”或“找不到匹配”一百万次. 我怎样才能为发布的整个句子分配一个真值或假值?然后我想添加类似....如果为 true 转储此帖子,如果为 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.";
        }
    }

    ?>
4

2 回答 2

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;
}
于 2013-10-04T10:54:55.330 回答
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
    }
}
?>
于 2013-10-04T10:54:05.517 回答