Jump to content

bad word filter


.josh

Recommended Posts

<?php
/*******************************************************************************
**Description:

   returns string with all the words filtered, preserving the non-masked
   characters' case in the matched word. 

**Syntax:

   string filter(string $str[, bool $is_whole_word]); 

**Parameters:

   str           - The string to be filtered
   is_whole_word - If set to TRUE, only whole words will be filtered. If set to
                   FALSE, it will match words within words. Defaults to TRUE if
                   not specified.

**Examples:

   echo filter("FucK you moTherfUckeR"); 
   // output: F*cK you moTherfUckeR

   echo filter("FucK you moTherfUckeR", false); 
   // output: F*cK you moTherf*ckeR   

**Internal Syntax & Parameters:

   $wordlist = array($key1 => $value1[, $key2 => $value2, [etc..]]]); 

   key   - The word to search for.
   value - The filter for the key. Even if is_whole_word is set to FALSE, only the
           the key substring of the word will be masked. In principle, anything 
           that evaluates (strtolower($value{$pos}) == strtolower($badword{$pos})) 
           to FALSE will be considered a mask.      

   Examples: key: sex
   value | badword | replacement 
   s3x   | sex     | s3x
   s3x   | SEX     | S3X
   *     | sex     | *
   ****  | sex     | ****
   s3x   | sexual  | s3xual
   s3x   | SeXual  | S3Xual
   *     | sexual  | *ual
   s3x   | asexual | as3xual
   *     | asexual | a*ual 

*******************************************************************************/

/*** function filter **********************************************************/
function filter($string, $wholeword = true) {

// list of words and filters
$wordlist = array('shit'    =>     'sh!t',
                  'suck'    =>     'svck',
                  'dang'    =>     'darn',
                  'fuck'    =>     'f*ck',
                  'sex'     =>      's3x',
                  'bitch'   =>    'b!tch',
                  'cunt'    =>     'cvnt',
                  'cock'    =>     'c0ck',
                  'ass'     =>     'arse',
                  'nigger'  =>    'negro',
                  'whore'   =>    'wh0re',
                  'dick'    =>     'd!ck');

// for each word in the list of words to replace...
foreach ($wordlist as $word => $filter) {
   // find all instances of current bad word
   $word = ($wholeword == true)? "/\b{$word}\b/i" : "/{$word}/i";
   preg_match_all($word, $string, $matches);
   // for each bad word found....
   foreach ($matches[0] as $bword) {
      // init the string (so it resets each loop iteration)
      $replacement = '';
      // find out how many letters are in the filter
      $count = strlen($filter);
      // for each letter in the filter...
      for ($pos = 0; $pos < $count; $pos++) {
         // compare the current letter to the letter in the bad word
         // if it's the same (case insensitive), use the bad word letter
         // if it's not the same use the filter letter. 
         // In this manner, the replacement word is built
         $replacement .= (strcasecmp($filter{$pos},$bword{$pos}) == 0)? $bword{$pos} : $filter{$pos}; 
      } // end for pos
      // pattern slightly varies, depending on wholeword
      $pattern = ($wholeword == true)? "/\b{$bword}\b/" : "/{$bword}/";  
      // switch out the bad word with the replacement
      $string = preg_replace($pattern, $replacement, $string);
   } // end foreach match
} // end foreach wordlist

// return result
return $string;
} // *** End function filter **************************************************

// example use:
$string = 'sex sexual asexual';

echo "original: $string <br/>";
echo "whole word: " . filter($string) . "<br>"; 
echo "all matched: " . filter($string, false); 

/******** output: **************
original: sex sexual asexual
whole word: s3x sexual asexual
all matched: s3x s3xual as3xual
********************************/
?>

Link to comment
Share on other sites

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