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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.