2015-04-06 204 views
0

我不確定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://stackoverflow.com/questions/19178295/ check-if-string-contains-one-of-several-words) – rjdown

回答

1

你開始在那裏,與$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; 
} 
0

你需要改善你的支票與字界,或者你會有像「手套」(愛)和「艾塞克斯」(性別)的話誤報。你也應該讓它不區分大小寫。

以下方法(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