2013-04-29 63 views
-2

在我的論壇中我想檢查一個字符串(發佈文本和帖子標題)爲壞詞。這些詞不應該被替換。當第一個壞字是數字時,做一些事情。我不想使用foreach工作,因爲foreach會對發現的每個不好的單詞做迴應。我只需要一個計數。如果算一個壞詞,然後>去在論壇中的不良詞列表(preg match)只能算

我已經

$str = $row['post_text'].$topic_data['topic_title']; 
$bad = array('badword','badword2'); 
if (preg_match($str,$bad)){ 
echo 'true'; 
}else{ 
echo 'false'; 
} 

這並沒有work..thank求助

+0

「沒有工作」背後的含義是什麼? – DonCallisto 2013-04-29 12:30:03

+0

要非常小心不要使[clbuttic錯誤](http://www.urbandictionary.com/define.php?term=clbuttic)。 – Spudley 2013-04-29 12:31:46

+1

preg_match是一個正則表達式匹配,你的$ str不是一個正則表達式。 – 2013-04-29 12:33:33

回答

0

使用爲每個壞詞陣列,並檢查它作爲一個stristr( )的$ str,在成功退出for each $ withword = true時,則可以在循環後發佈一個響應,如果$ badword == true。

+0

嗨,Al,這聽起來對我來說很好,那就是我想要的。你能給我一些代碼嗎?非常感謝。 – labu77 2013-04-29 12:39:36

+0

http://php.net/manual/en/control-structures.foreach.php和http://uk1.php.net/manual/en/function.stristr.php – 2013-04-29 12:41:41

+0

不錯,2鏈接。我喜歡stackoverflow,因爲我經常複製和粘貼解決方案。當你這麼容易,然後請給我一個解決方案... $ comment = $ row ['post_text']。$ topic_data ['topic_title']; $ fillout = array(「badword1」,「badword2」,「badword3」); $ clean = true; preg_match(「/".$ fillout。」/「,$ comment,$ matches);如果(count($ matches)> 0) { echo'true'; $ clean = false; } 如果($乾淨) {// 插入不要} $ – labu77 2013-04-29 12:46:41