Jump to content

Bad word filter


dk4210

Recommended Posts

Hello Guys,

 

I need some help  here.. I want to implement a Bad word filter and not quite sure how to do it..

 

I am grabbing the following vars from the text fields

 

$ad_title = filter_var($_POST['ad_title'], FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES);

$ad_body = filter_var($_POST['description'], FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES);

 

Then its inserted into the db.

 

I would like to run it through this function first..

 


FUNCTION BadWordFilter(&$text, $replace){


     // fill this array with the bad words you want to filter and their replacements
     $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
          }     
     }
}

// this will replace all bad words with their replacements. $any is 1 if it found any
$any = BadWordFilter($wordsToFilter,1); 

 

 

I really would also like to query a table for any words that match and then use a replacement like [censored]

 

Any help would be greatly appreciated!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/230044-bad-word-filter/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.