tags:

views:

65

answers:

1

Possible Duplicate:
How do you implement a good profanity filter?

I have an iPhone app that passes a string through a URL to my php script, which then inserts to my database. I'd like to write some php code that reads $body for any pre-defined profanity.

Right now, I only have the following code, but it only looks for exact matches, not if $body contains, then.. type of a sequence. Also, I need it to replace the swear with a blank " ", removing the word all together.

if($body == "swear word") 
    return;
+2  A: 
$swears = array("shoot", "darn", "heck");

foreach ($swears as $bad_word)
    $body = str_replace($bad_word, " ", $body);

This will work as a quick-and-dirty solution, although it does have the Scunthorpe Problem (e.g. "heckler" will get transformed to "ler" if "heck" is on your profanity list).

eliah
This is a [clbuttic mistake](http://thedailywtf.com/Articles/The-Clbuttic-Mistake-.aspx) ;)
Daniel Vandersluis
True enough. Edited in something to that effect.
eliah
Yes, I'm aware of the Scunthorpe Problem. How can you make the array case insensitive?
BigMike
Use `str_ireplace` instead of `str_replace` if you want to match case-insensitively.
eliah