Bad word filter and replace


<?php
$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
";
echo "whole word: " . filter($string) . "
";
echo "all matched: " . filter($string, false);

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

6 Comments

  1. June 17, 2009 at 12:07 pm

    Nice Articles.
    Can I have your contact details? Need to contact you for something interesting.
    You can send me an email.
    Thanks

  2. asdfasd said,

    July 27, 2009 at 5:49 pm

    sex

  3. cv jeevaa said,

    July 8, 2010 at 10:04 am

    Hi,
    I am working in businessgyan website in drupal. In an article if there is any bad word i want to convert into string like **** . I am uploaded the module abuse and enabled.Then I tried I didnt get it. Please help me how to solve this problem

    regards
    cv jeevaa
    bangalore
    native: madurai, TN

    • Chandra Sekar Vinod Kumar said,

      July 9, 2010 at 3:42 am

      Could u plz tell me what version you installed and what module version u used for this?

  4. Kenton Bumby said,

    October 27, 2021 at 5:23 pm

    Having read this I thought it was really enlightening. I appreciate you taking the time and effort to put this short article together. I once again find myself spending a significant amount of time both reading and posting comments. But so what, it was still worthwhile!

  5. October 29, 2021 at 6:04 am

    Hi, I do think this is an excellent blog. I stumbledupon it 😉 I may return yet again since I saved as a favorite it. Money and freedom is the best way to change, may you be rich and continue to help others.


Leave a comment