我有一个过滤器坏字代码我想用.txt文件替换这个ARRAY,这样我就可以把所有的坏单词都放进txt文件中,或者有没有办法使用MYSQL数据库来存储坏单词,然后从那里调用?
FUNCTION BadWordFilter(&$text, $replace){
$bads = ARRAY (
ARRAY("butt","b***"),
ARRAY("poop","p***"),
ARRAY("crap","c***")
);
IF($replace==1) { //we are replacing
$remember = $text;
FOR($i=0;$i<sizeof($bads);$i++) { //go through each bad word
$text = EREGI_REPLACE($bads[$i][0],$bads[$i][1],$text); //replace it
}
IF($remember!=$text) RETURN 1; //if there are any changes, return 1
} ELSE { //we are just checking
FOR($i=0;$i<sizeof($bads);$i++) { //go through each bad word
IF(EREGI($bads[$i][0],$text)) RETURN 1; //if we find any, return 1
}
}
}
$qtitle = BadWordFilter($wordsToFilter,1);
我刚刚开发了一个函数,可以过滤掉不好的单词
function hate_bad($str)
{
$bad = array("shit","ass");
$piece = explode(" ",$str);
for($i=0; $i < sizeof($bad); $i++)
{
for($j=0; $j < sizeof($piece); $j++)
{
if($bad[$i] == $piece[$j])
{
$piece[$j] = " ***** ";
}
}
}
return $piece;
}
并这样称呼它
$str = $_REQUEST['bad']; //'bad' is the name of the text field here
$good = hate_bad($str);
if(isset($_REQUEST['filter'])) //'filter' is the name of button
{
for($i=0; $i < sizeof($good); $i++)
{
echo $good[$i];
}
}
Yes制作一个类似bad_words.txt的文件,其中包含以下条目(请注意,每个单词组合都在单独的一行上):
butt,b***
poop,p***
crap,c***
然后将该文件读取到一个数组中,如下所示:
$file_array = file('/path/to/bad_word.txt',FILE_IGNORE_NEW_LINES);
然后要创建一个像$bads数组这样的数组,请执行以下操作:
$bads = array();
foreach ($file_array as $word_combo) {
$bads[] = explode(',', $word_combo);
}
希望这能有所帮助。
您可以执行以下任一操作。。。
您可以使用file_get_contents()
之类的东西从文件中读取,或者使用MySQL API来查询数据库中的坏单词。
您是否设置了数据库架构?此外,不赞成使用eregi_replace()
。请改用preg_replace()
。
您可以使用MYSQL。
只要有一个包含两列的表:单词和替换。
然后在代码中,您需要连接到数据库并读取每一行。但是您可以将每一行存储在一个数组中。
结果将与您当前的数组结构非常相似。
要连接到数据库,请使用下面的教程。http://www.homeandlearn.co.uk/php/php13p1.html