简体   繁体   中英

PHP filter bad words

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); 

I just developed a function that can filter out the bad words

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;
}

and call it like this

$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];
    }
}

You can do either...

You can use something like file_get_contents() to read in from a file, or use a MySQL API to query your database for bad words.

Do you have a database schema set up? Also, eregi_replace() is deprecated. Use preg_replace() instead.

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',FILE_IGNORE_NEW_LINES);

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.

You could use MYSQL.

Just have a table with two columns: the word and the replacement.

Then in your code, you will need to connect to the database and read in each row. But you can then store each row in an array.

The result would be very similar to your current array structure.

To connect to a database, use the tutorial below. http://www.homeandlearn.co.uk/php/php13p1.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM