user1528878
user1528878

Reputation: 41

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

Upvotes: 1

Views: 8577

Answers (4)

Mike Brant
Mike Brant

Reputation: 71384

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.

Upvotes: 1

Saikelu
Saikelu

Reputation: 71

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

Upvotes: 4

user1027562
user1027562

Reputation: 265

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.

Upvotes: 1

Antony D&#39;Andrea
Antony D&#39;Andrea

Reputation: 1004

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

Upvotes: 0

Related Questions