不确定strpos()是否正确使用

不确定strpos()是否正确使用

问题描述:

我不确定strpos()是否适用于此 任务。不确定strpos()是否正确使用

问题:

如果用户输入的仇恨或我的垃圾邮件可变 另一个字符串其返回的垃圾邮件过滤器的消息是正确的,但如果用户 输入垃圾邮件的变量,任何字符串不混各种它 传递进行处理。

我想要的输入,从第一个字符串的最后一个字符串 检查和t不与任何包含垃圾邮件变量字符串,然后 返回过程中,这里是我的代码

<?php 
    //messgae 
    error_reporting(E_ALL^E_NOTICE); 
    $msg = array(); 
    $spam = "hate partisan party kill maim murder violence love sex fight beat assasinate thug steal sell bribe protest baricade bullets militia fear "; 
    $spam_array = explode(" ",$spam); 

    $check = strpos($spam, $_POST['message']); 

     if ($check == true) { 
     //do nothing 
     $msg['invalid'] = 'Spam filter test didnt allow your message'; 

     } else { 

     $msg['valid'] = 'process'; 
     } 


    if(isset($_POST['send'])){ 

     $message= $_POST['message']; 
    } 

    ?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Strpos</title> 
    </head> 

    <body> 
    <?php 
    if (isset($msg)) { 
    echo '<ul>'; 
    foreach ($msg as $alert) { 
    echo "<li class='warning'>$alert</li>\n"; 
    } 
    echo '</ul>'; 
    }?> 
    <form action="" method="post"> 
    <input name="message" type="text" /> 
    <input name="send" type="submit" value="Submit" id="send" /> 
    </form> 
    </body> 
    </html> 
+0

检查''对== FALSE'代替 – Ghost

+0

您使用strpos()不正确strpos'。它可以返回一个整数0,即'== false',但这只是表示它在'haystack'字符串的开始处找到了您的搜索字符串。此外,这种过滤器基本上是无用的:http://en.wikipedia.org/wiki/Scunthorpe_problem –

+0

可能重复[检查字符串是否包含几个单词之一](http://*.com/questions/19178295/ check-if-string-contains-one-of-several-words) – rjdown

你开始在那里,与$spam_array。 他们检查它知道,你检查是否在你的消息中找到了错误的单词的确切字符串。

stripos而不是strpos,这样它将是不区分大小写的。

$spam = "hate partisan party kill maim murder violence love sex fight beat assasinate thug steal sell bribe protest baricade bullets militia fear "; 
$spam_array = explode(" ",$spam); 
$isSpam = isSpam($_POST['message'], $spam_array); 


function isSpam($content, $spamList) 
{ 
    foreach($spamList as $badWord) { 
     if(stripos($content, $badWord) !== false) { 
      return true; 
     } 
    } 

    return false; 
} 

你需要改善你的支票与字界,或者你会有像“手套”(爱)和“艾塞克斯”(性别)的话误报。你也应该让它不区分大小写。

以下方法(check函数)在查找消息中的每个“垃圾邮件词”时使用带有字边界元字符的preg_match。该i修改也使得它不区分大小写:

function check($msg, $spam_array) { 
    foreach ($spam_array as $spam_word) { 
     if (preg_match("/\b" . $spam_word ."\b/i", $msg)) { 
      return false; 
     } 
    } 
    return true; 
} 

function test($msg, $spam_array) { 
    echo "$msg => ", check($msg, $spam_array) ? 'OK' : 'not OK', PHP_EOL; 
} 

$spam = "hate partisan party kill maim murder violence love sex fight beat " 
    . "assasinate thug steal sell bribe protest baricade bullets militia fear"; 
$spam_array = explode(" ", $spam); 

test("I'm known for my cookie munching skills.", $spam_array); 
test("I could kill for some cookies right now!", $spam_array); 

输出:

I'm known for my cookie munching skills. => OK 
I could kill for some cookies right now! => not OK