Richard Davey wrote:
> Hi all,
> 
> Just wanting to pick your collective brains on this one:
> 
> How do you go about implementing a swear / bad-word filter in PHP?
> 
> Reasons for needing one aside, I'm just wondering if you favour a
> regexp, a substr_count, or what? Do you like to *** out the bad words,
> or just error back to the user? (I guess that is application
> specific).
> 
> There are always ways to circumvent them, but I'm still curious to
> know how you prefer to handle it.

We implemented a simple key => value array map of rude words to
"sanitised" equivalents:

e.g. breast => sandwich, felch => retrieve etc. etc.

Then we used implode to generate a regexp:

  $new = ' '.$strBadWordsWrittenByEvilUser.' ';
  $gStopRegexp = '('.implode('|',array("\n","\r",'
',',','\.',';',':','-','\!',"'",'"')).')';
  foreach ($gNaughtyWords as $naughty=>$nice)
  {
    $regexp = $gStopRegexp.'('.$naughty.')([s]{0,1})'.$gStopRegexp;
    $regexp_rep = '\1'.$nice.'\3\4';
    $new = mb_eregi_replace($regexp, $regexp_rep, $new);
  }

  return trim($new);


The added bonus is that you can flip the key/values in your array and
turn perfectly nice sentences into torrents of abuse ;)


It's not perfect but it works for the most part. And it's fun to think
of all the rude words people might use!!

HTH

Col

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to