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
********************************/
?>

Advertisements