Snippet Name: Bad Word Filter
Description: Replaces bad words, censoring the text passed in automatically.
Also see: » Censor Message Text
» Vulnerability Tester
» Block IP Addresses
» Ban Proxy Servers
» Anti-Flood Protection
» Anti-SQL Injection Function
» XSS Sanitizer Function
» Filter non-alphanumeric characters
Comment: (none)
Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009
|
<?
// BadWordFilter
// This function does all the work. If $replace is 1 it will replace all bad words
// with the wildcard replacements. If $replace is 0 it will not replace anything.
// In either case, it will return 1 if it found bad words or 0 otherwise.
// Be sure to fill the $bads array with the bad words you want filtered.
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);
// this will not repace any bad words. $any is 1 if it found any
$any = BadWordFilter($wordsToFilter,0);
?> |