2012-05-09 47 views
1

我已經設定該功能php字符過濾器功能?

function bad_words($val){ 
    global $pre; 
    $sql = mysql_query("SELECT * FROM " . $pre . "BAD_WORDS") or die(mysql_error()); 
    $rs = mysql_fetch_assoc($sql); 
    if (mysql_num_rows($sql) > 0) { 
     $bad_words = $rs['BAD_WORD']; 
     $replace = $rs['REPLACE']; 
    } 
    $val = str_ireplace($bad_words, $replace, $val); 
    return $val; 
} 

BAD_WORDS Table (ID, BAD_WORD, REPLACE)


當我使用這個功能,它取代字與ID = 1,但不會取代其他的話。

我錯過了什麼?

+3

[。如果你想在褻瀆過濾器的設想一些反饋](http://www.codinghorror.com/blog/2008/10/obscenity-filters-bad -idea-or-illredibly-intercoursing-bad-idea.html) –

+0

'$ pre'變量中的內容是什麼? – Ibu

+1

我也認爲'全球'是一個壞詞。 'mysql_ *'以及順便說一句。 – PeeHaa

回答

2

您只提取表中的第一行。您必須使用循環遍歷結果集中的所有行,才能遍歷 。

function bad_words($val) 
{ 
global $pre; 
$sql = mysql_query("SELECT * FROM " . $pre . "BAD_WORDS") or die(mysql_error()); 

if (mysql_num_rows($sql) > 0) { 
    while($rs = mysql_fetch_assoc($sql)) { 
     $bad_words[] = $rs['BAD_WORD']; 
     $replace[] = $rs['REPLACE']; 
    } 
} 
$val = str_ireplace($bad_words, $replace, $val); 
return $val; 
} 
+0


thar的工作100%
謝謝 –