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,477 Responses to “Detecting Bad Words PHP (Filter bad words)”

  1. sekolah says:

    Hello…

    Came across your blog and decided to have a very quick study, I don’t always do this, but I found this blog excellent. Wonderful to see a Blog that has great content, and understandable. Awesome blog…

  2. Cool sites…

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

  3. Hello…

    I do trust all the ideas you’ve introduced on your post. They are really convincing and can certainly work. Nonetheless, the posts are too quick for novices. May just you please extend them a bit from next time? Thanks for the post….

  4. Check out…

    Thanks, I’ve recently been seeking for details about this subject matter for ages and yours is the best I’ve discovered so far….

  5. Online Article……

    [...]The information mentioned in the article are some of the best available [...]……

  6. Websites worth visiting…

    [...]here are some links to sites that we link to because we think they are worth visiting[...]……

  7. D&G says:

    Websites we think you should visit…

    [...]although websites we backlink to below are considerably not related to ours, we feel they are actually worth a go through, so have a look[...]……

  8. Links…

    [...] this is actually the end of this short article. Here you can find several sites that we feel you will enjoy, simply click the links through [...]…

  9. Online Article……

    [...]The information mentioned in the article are some of the best available [...]……

  10. Dominical says:

    Cool sites…

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

  11. The read was interesting, stay in touch……

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

  12. Website worth visiting…

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

  13. Visitor recommendations…

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

  14. yeast infection are very similar to that…

    of a females, there will usually be soreness found to affect the head of the penis, this can be followed by itchiness much like with a women’s yeast infection.just a reminder, that i have the full report of yeast infection symptoms,…

  15. Can’t Get Any Better Than This…

    [...]Right on the spot! I have been looking for this. Stay the best[...]……

  16. Superb website…

    [...]always a big fan of linking to bloggers that I love but don’t get a lot of link love from[...]……

  17. Cool sites…

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

  18. Insulated Concrete Forms…

    [...]Here is a superb Blog You might Locate Fascinating that we Encourage You[...]…

  19. Properties For Sale in Ontario…

    [...]that may be the end of this write-up. Right here you?ll locate some internet sites that we believe you?ll enjoy, just click the hyperlinks over[...]…

  20. Lump says:

    men…

    [...]By doing so, they are not exposing themselves to the negative effects of heartburn[...]…

  21. Visitor recommendations…

    [...] we came across a very good site that you may possibly enjoy. Look it over if you expect [...]…

  22. Recent Blogroll Additions…

    I saw this really great post today….

  23. You should check this out…

    I saw this really great post today….

  24. Recent Blogroll Additions…

    I saw this really great post today….

  25. You should check this out…

    I saw this really great post today….

  26. kolonie says:

    You should check this out…

    I saw this really great post today….

  27. Great information…

    This can be amazing. So we watch every single peace of mind and we are impressed. We are precisely attracted to one of these viruses. Device appreciate property insert, and assess doing while in this. Please keep editing. They may be seriously high-pri…

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