PHP Bad Words Filter !

Get rid of bad words before they get rid of your visitors with PHP naughty word replacement.

What is it?

Ok, so you have a comment or shout box on your website that allows visiting folk to leave a message or blog comment for you and others to read. Fine, always a good idea to make your site interactive as it makes your visitors experience with you a lot more enjoyable. But, what if you get someone who is having a particularly bad day, or strongly dissagrees with something you (or someone else) has said, and decides that he wants to rant, swear, call you names and or generally verbally abuse you ? Can you stop him doing it ? Not really. Should you stop him doing it ? Again, not really (freedom of speech and all that). So the only option you are left with is to try and clean up what has been written... Enter the Bad Words Filter.

What does it do?

Replaces potentially rude or offensive words with a text string of your choice (e.g. 'Naughty, naughty!').

Below is the PHP code we at Grow use to try (ahem!) and filter out any unaceptable words (or phrases) that may be left by our visitors. In a nutshell, the function is called after you have extracted the comment text from your DB and before you echo said text to your page. The words that you wish to filter are stored in the $badwords array (this can be added to as you see fit), and the words (or in our case the phrases) that you wish to display instead are stored in the $replacements array. When the function is running, it loops through all of the entries in the badwords array (using a 'for' loop), and if it finds a 'badword' (case insensitive) it will replace it from within the passed-in string with a random entry from the $replacements array, and then finally the corrected string will be returned to you for displaying.

That's it.... quick, clean, and simple, and does exactly what it says on the tin !

Please Note: Below are a list of words that some people may find offensive. It is not our intent to offend any of our visitors, indeed that is the reason behind this function, but the code must be shown in it's entirity if it is to be understood.

function filterBadWords($str){

 // words to filter
 $badwords=array( "[naughty word removed]", "[naughty word removed]", "[no swearing please]", "[oops]", "[oops]", "[naughty word removed]", "[oops]", "[oops]" );

 // replace filtered words with
 $replacements=array( "[naughty word removed]", "[how wude!]", "[no swearing please]" );

 for($i=0;$i < sizeof($badwords);$i++){
  $rand_key = (rand()%sizeof($replacements));
  $str=eregi_replace($badwords[$i], $replacements[$rand_key], $str);
 return $str;

example call : $your_text_on_page = filterBadWords($your_text_from_comments);

If you look at the first comment below, you will see the script in action :)

More articles


This article is no longer accepting comments.

  • On the 17th Aug 2005 at 23:23 GMT, Paul Whitrow wrote:

[oops] the [oops] [oops]s..!

  • On the 27th Aug 2005 at 00:49 GMT, Jon Tan wrote:

I wonder if we've missed any bad words?