2013-08-03 138 views
-3

我想检查表单字符串中的单词,并用sql数据库控制检查单词。这是我到目前为止:str替换函数

while ($bad_w = mysql_fetch_array($result_badw)) { 
    $newmessage = str_replace($bad_w['word'],"****",$org); 
} 

有关如何纠正它的任何想法?

+1

有什么问题你面对? –

+0

只看代码,即使没有解释也很明显。 – MightyPork

+0

@MightyPork addited .. –

回答

2

您反复使用原来的字符串在你的替换。您应该覆盖字符串
$org = str_replace($bad_w['word'],"****",$org))以确保所有单词都被过滤。

只希望没人谈论你的论坛约黑执事:对

+0

谢谢你! :)工作.. –

0

您每次更换新单词时都会覆盖$newmessage

您还应该使用str_ireplace(),这是不区分大小写的 - 这将是审查好。

尝试像这样:

$newmessage = $org; 

while($row = mysql_fetch_array($result_badw)) { 
    $newmessage = str_ireplace($row['word'], "****", $newmessage); 
} 

或者,如果你想在相同数量的*因为是在恶劣的字字母:

$newmessage = $org; 

while($row = mysql_fetch_array($result_badw)) { 
    $bword = $row['word']; 
    $repl = str_repeat('*', strlen($bword)); 

    $newmessage = str_ireplace($bword, $repl, $newmessage); 
} 
+0

谢谢你..现在为我工作:) –

+0

编辑它,确保你使用'str_ireplace' – MightyPork