Script Perfect

         Random snips of code and bugs

Detecting Bad Words PHP (Filter bad words)

Posted by Tim On September - 17 - 2009

Simple and easy, stop most of the nasty words from appearing on your website or blog with one simple function. This has stemmed from multiple users posting profanity on another one of our sites. After searching far and wide for an easy solution, alas we could not find one. This resulted in a few hours behind the keyboard plucking away with some trial and error, the end result is a lightweight function that works great.

The hardest part about developing a function that would detect profanity was accounting for the special characters being used as letters. Like the “#” being used for a “H” or a “$” for a “S”. The solution to detect the special characters was rather simple once we figured out a good method. Now, in it’s entirety, it will stop most profanity and if any gets through it is very easy to modify it for additional characters and words.


This complete function(with a nice list of bad words) as well as a demo can be downloaded here:
Bad Words Filter – zip
- or –
Try the demo, Click Here

The Setup

We will begin by creating a function called BadWords($str) where $str will be the input string to be evaluated. Once we receive the string we will then convert it to lower case and store it in another variable, this is so we can check the original string as well as a string after we replace all of the special characters.

/**
* @author Timothy Sturrock
* @website www.scriptperfect.com
* @license Creative Commons: Attribution-Share Alike 3.0 Unported
* @use You are free to use the following for any purpose as long as the author and website are not removed.
* @copyright 2009
*/

function BadWords($str){
$str=strtolower($str);
$cleanstr = $str;
}

Special Characters

Detecting special characters is accomplished through the use of array, we assign a character or a series of characters to a letter that it would represent starting with the most complicated to the least. Sometime people have a habit of using two special characters such as “|3″ to make letters, that would be a “b”. We are sure that there are many more that can be thrown into the mix here but this will show you the basics we have come up with:

$charlist = array("|3"=>"b", "13"=>"b", "l3"=>"b", "|)"=>"d", "1)"=>"d", "[)"=>"d", "|("=>"k", "1("=>"k", "$"=>"s", "("=>"c", "1"=>"i", "+"=>"t", "|"=>"i", "!"=>"i", "#"=>"h", "<"=>"c", "@"=>"a", "0"=>"o", "{"=>"c", "["=>"c");

Replacing the Caharacters

A simple loop will parse through each of the special characters in the string and replace any of the characters in the array above with their corresponding letters. We will store this into a new variable which will be checked later on along with the original string. Here is how we replace all of the characters:

foreach($charlist as $char=>$value){
$cleanstr = strtolower(str_replace ($char, $value, $cleanstr));
}

Looking for the Filth

So now all that remains is to make a list of the words we are looking for (I have included a nice list in the demo files) separated by commas.

$words = "all,our,bad,words,go,here";

Next we will take that list and explode it using the “,” as our divider. All that is left is to parse through each of the words and try to detect them in our original input string and our string with all the characters that were replaced:

$badarray = explode(",", $words);
foreach ($badarray as $naughty) {
if (preg_match("/$naughty/", $str) or preg_match("/$naughty/", $cleanstr)){
return true;
}
}
return false;

That’s it, simple and effective. On the down side, if your input string is large it may take a bit more time to loop through all the words and characters.

Please post suggestions on how to improve this script and if you have any ideas on how to implement a feature which will replace the bad word with a character such as “*”, but remember to find the string position the special characters have to be taken into consideration.

This complete function(with a nice list of bad words) as well as a demo can be downloaded here:
Bad Words Filter – zip
- or -
Try the demo, Click Here

3,485 Responses to “Detecting Bad Words PHP (Filter bad words)”

  1. Interesting…….

    A very neat post….

  2. Read was interesting, stay in touch……

    [...]please visit the sites we follow, including this one, as it represents our picks from the web[...]……

  3. Jerseys says:

    Interesting…….

    A very unique post….

  4. Great information…

    This is certainly very good. Nutritionists stare upon this unique written copy therefore we are shocked. We are attracted to this type of activities. Our team appreciate body information and facts, and enjoy doing while in this. Please keep adding info…

  5. Thumbs up…

    I saw this really great post today….

  6. Sources……

    [...] Here is an awesome blog you might find interesting that we suggest you [...]…

  7. Visors says:

    Interesting…….

    A very unique post….

  8. Posts to Save…

    [...]below you’ll find the link to a few websites that we think you should take a glance at[...]……

  9. panasonic sd says:

    Sources…

    [...]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[...]……

  10. TV Stands says:

    Hey…

    I think this site has got some really great information for everyone. “In this world second thoughts, it seems, are best.” by Euripides….

  11. 4 x 4 site says:

    Recent Blogroll Additions……

    [...]usually posts some very interesting stuff like this. If you’re new to this site[...]……

  12. Check this out…

    [...] that is the end of this article. Here you’ll find some sites that we think you’ll appreciate, just click the links over[...]……

  13. Calvin Klein says:

    Gems form the internet…

    [...]very few websites that happen to be detailed below, from our point of view are undoubtedly well worth checking out[...]……

  14. Bookmarks says:

    Related……

    [...]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[...]……

  15. Visitor recommendations…

    [...]one of our visitors recently recommended the following website[...]……

  16. Sites we Like……

    [...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]……

  17. Great information…

    This is certainly first-class. Some watch this unique product therefore we are staggered. We are most certainly curious about this kind of points. Just one appreciate property struggle, and value for money your precious time while in this. Please keep …

  18. Recent Blogroll Additions……

    [...]usually posts some very interesting stuff like this. If you’re new to this site[...]……

  19. barn says:

    Recent Blogroll Additions……

    [...]usually posts some very interesting stuff like this. If you’re new to this site[...]……

  20. Hello…

    Thanks a lot for giving everyone such a remarkable possiblity to read in detail from here. It is usually very fantastic and also stuffed with a lot of fun for me and my office acquaintances to visit your site nearly 3 times in one week to study the fre…

  21. download says:

    Read was interesting, stay in touch……

    [...]please visit the sites we follow, including this one, as it represents our picks from the web[...]……

  22. Recommeneded websites…

    [...]Here are some of the sites we recommend for our visitors[...]……

  23. Sources…

    [...]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[...]……

  24. Hello…

    I like this weblog very much, Its a real nice office to read and find info. “Whenever I hear, ‘It can’t be done,’ I know I’m close to success.” by Michael Flatley….

  25. You should check this out…

    [...] Wonderful story, reckoned we could combine a few unrelated data, nevertheless really worth taking a look, whoa did one learn about Mid East has got more problerms as well [...]……

  26. Blogs to read…

    [...] If you are searching for additional info about this situation, more information about today’s problem is available on [...]…

  27. Dancers says:

    Check this out…

    [...] that is the end of this article. Here you’ll find some sites that we expect you’ll appreciate, just follow the links over[...]……

  28. Luck says:

    Man…

    [...]It has to produce stronger digestive acids in order to break down foods which are not easily digestible[...]…

  29. Sources…

    [...]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[...]……

  30. Awesome website…

    [...]the time to read or visit the content or sites we have linked to below the[...]……

  31. Related……

    [...]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[...]……

  32. Read was interesting, stay in touch……

    [...]please visit the sites we follow, including this one, as it represents our picks from the web[...]……

  33. You should check this out…

    This one was very handy, exactly what I was lookin’ for…

  34. Blogs ou should be reading…

    [...]Here is a Great Blog You Might Find Interesting that we Encourage You[...]……

  35. You should check this out…

    [...] Wonderful story, reckoned we could combine a few unrelated data, nevertheless really worth taking a look, whoa did one learn about Mid East has got more problerms as well [...]……

  36. Gems form the internet…

    [...]very few websites that happen to be detailed below, from our point of view are undoubtedly well worth checking out[...]……

  37. darmowe filmy…

    Bardzo ogromna cipka z wibratorem w szparce….

  38. Great information…

    This is certainly very nice. One stare upon this written material therefore we are startled. We are attracted to this sort of meals. Human beings appreciate your old involvement, and significance your precious time with this. Please keep add relevant c…

  39. auto quotes says:

    Cool site…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  40. Website worth visiting…

    below you’ll find the link to some sites that we think you should visit…

  41. Hello…

    I was searching the internet checking out some stuff and came across your site. I just wanted to tell you that I think your site has some good content and that I have already favorited this site so I don’t lose it Thanks!…

  42. important links…

    Guys check out these important links…

  43. News info…

    I was reading the news and I saw this really cool topic…

  44. Cinetube says:

    [...]we like to honor many other internet sites on the web, even if they aren’t linked to us, by linking to them. Under are some webpages worth checking out[...]……

    [...]Here are some of the sites we recommend for our visitors[...]……

  45. Wow!…

    A very spectacular post….

  46. Cool sites…

    [...]we came across a cool site that you might enjoy. Take a look if you want[...]……

  47. Nice Stuff…

    [...]Glad I detected this on google[...]…

  48. ………..

    I guess in the meantime i’m going to be happy with book-marking and adding the Rss in order to my own Yahoo accounts. My spouse and i enjoy brand new improvements and definately will share this kind of blog using my Myspace party….

  49. proof that estrogen hair loss is a…

    common condition. why will that be? and what does estrogen have to do with hair loss? they say the lower the levels of estrogen are, the more hair loss a woman can experience. why is this and what do you need…

Leave a Reply

Spam protection by WP Captcha-Free

About Me

I am an independent web developer and webmaster of many sites. The main goal of Script Perfect is to provide answers to some of the hard to find questions when it comes to website design and coding.

Twitter