Re: bad word filter
| From: | Ryan Fischer | Date: | Tue, 24 Jul 2001 19:27:45 +0000 |
| Subject: | Re: bad word filter | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-59404@lists.php.net to get a copy of this message | ||
You wrote:
> I've been reading the "Profanity Filter" thread in the list archives
but
> haven't found anything real helpful. Here's my code so far, this of
course
> won't work.
> <?
> function filterWords($string, &$result) {
> $badwords="shit, fuck, ass, bitch";
> $word=explode(", ", $badwords);
> for ($i=0; $i<count($word); $i++) {
> $replace = str_replace("$word[$i]", "****", $string);
> }
> }
> $string = "Ass monkey";
> filterWords("$string", $result);
> print "$result";
> ?>
>
> How can I scan for the $badwords in $string and replace $badwords with
****?
Why waste your time? People will find a way around the filters anyway,
if they really want to use the words you're censoring. Just a point to
be made.
Anyway, here's what I would do:
function filterWords($str){
$badwords = array("shit", "fuck", "ass", "bitch");
for($i=0; $i<count($badwords); $i++){
$str = $eregi_replace("$badwords[$i]", "*****", $str);
}
return $str;
}
$str = filterWords("Ass monkey");
print $str; // Prints "***** monkey"
If you want to get really fancy, you can replace the "*****" with some
function calculating just how many asterisks you need to censor out the
current word for its length. HTH. :)
--
-Ryan :: ICQ - 595003 :: GigaBoard - http://www.gigaboard.net/