dora1989 2012-07-17 13:39
浏览 56
已采纳

PHP过滤坏词

I have a filter bad words codes below I want to replace this ARRAY with the .txt file so that I can put all the bad words into the txt file or is there any way to use MYSQL database to store the badwords and then call from there ?

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条回答 默认 最新

  • dtiu94034 2012-07-17 13:48
    关注

    Yes make a file like bad_words.txt with entries like (note each word combo is on a separate line):

    butt,b***
    poop,p***
    crap,c***
    

    Then read that file into an array like so:

    $file_array = file('/path/to/bad_word.txt');
    

    Then to create an array like your $bads array do this:

    $bads = array();
    foreach ($file_array as $word_combo) {
        $bads[] = explode(',', $word_combo);
    }
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库