SuperSatellite ||

Subscribe (?) Subscribe to RSS

Posts Tagged ‘profanity’

Creating a profanity filter

Published on December 6th, 2007 in 4 Comments »

So, today I wrote the dirtiest function I have ever written in PHP. I typed more profanity at once than I think I ever have in the past. As part of a new AJAX (Asynchronous Javascript And XML) live search query displayer I wrote for our Google Mini, I had to do some filtering to make sure naughty phrases wouldn’t show up. This is a pretty straightforward script you could incorporate into different applications, like a shoutbox, or comment form.

Alternatively, you could swap in preg_replace() or eregi_replace() instead of preg_match() and censor phrases that way. In this example, I use preg_match() just to test the query, and if the filter matched, I excluded the query from display entirely. I have this set to match anything that occurs in a query, so if the word “butt” was a filter term, it would catch “butts,” “butthole,” and “buttmunch.” That saved a lot of extra typing and filtering. Yes, it increases the likelihood of a false positive, but in this case we weren’t too concerned about an overly aggressive filter.

If the filter makes a match, it returns a boolean value of true. From there, do as you will. You could build in your own handler code as well (especially if you just wanted to censor individual words).

If you have a better idea or refinement, comment below and I can tweak this appropriately.

  1. function profanityFilter($query) {
  2.   /* Set filter terms to exclude from display, including
  3.   word roots or partials. Can be regular expressions. */
  4.   $filter = array("word1","word2","word3");  
  5.  
  6.   for ($i = 0; $i < sizeof($filter); $i++) {
  7.     /* Look for a regex match, case insensitive */
  8.     if (preg_match("/".$filter[$i]."/i", $query)) {
  9.       /* Return a match, or put in your own handler */
  10.       return true;
  11.     }
  12.   }
  13. }
Click to Download This Theme

Login/Register

Sign in with OpenID
Don't have OpenID? Get one here.
(What is OpenID?)
My Vidoop More secure than passwords.

My Tweets

  • ...Back from the retreat. I have a strange knot in my wrist. It is unpleasant. 1 hr ago
  • ...Getting in some CMS bugfixes before having to suffer from a department "retreat" tomorrow. Retreat? More like punishment. Why me? 21 hrs ago
  • ...Changed channels on my wireless router. Much better signal strength now. Wish it was smart enough to find the cleanest frequency itself. 23 hrs ago
  • More updates...

Posting tweet...

Enjoying...

2001 ManiacsCabin Fever

The Sound Of Madness Limited Fan Club EditionIt's Not News, It's Fark: How Mass Media Tries to Pass Off Crap As News

My Stuff



Archives

My Zimbio Buddhist Blogs >