1

我在下面有一个过滤坏词代码我想用 .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); 
4

4 回答 4

4

我刚刚开发了一个可以过滤掉坏词的功能

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];
    }
}
于 2013-02-04T06:53:05.647 回答
1

你可以做任何...

您可以使用类似file_get_contents()从文件中读取的内容,或使用MySQL API来查询数据库中的坏词。

您是否设置了数据库架构?此外,eregi_replace()已弃用。改为使用preg_replace()

于 2012-07-17T13:46:23.837 回答
1

是的,制作一个像 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);
}

希望这可以帮助。

于 2012-07-17T13:48:31.160 回答
0

你可以使用MYSQL。

只需有一个包含两列的表:单词和替换。

然后在您的代码中,您将需要连接到数据库并读取每一行。但是您可以将每一行存储在一个数组中。

结果将与您当前的数组结构非常相似。

要连接到数据库,请使用以下教程。 http://www.homeandlearn.co.uk/php/php13p1.html

于 2012-07-17T13:43:43.580 回答