Almost Obvious
Posted by tim in I hate technology. on December 6, 2006

One thing I see a lot of on some programming forums I visit is, "How do I create a 'bad words' system? How do I filter out bad words from a user input string?"

Well, there's more than one way to skin a cat, but here's how I would do it (All code is in PHP unless otherwise specified):

Step 1: Establish an array of "bad words". $badwords = array('word1', 'word2', 'word3');

Or, to make it a little easier to add to later: $badwords[] = 'word1'; $badwords[] = 'word2'; $badwords[] = 'word3';

Step 2: Compare your input string to the bad words list. Okay, there's a couple ways of doing this... I'll do it the verbose way:

$words = pregsplit('/\s+/', $input, -1, PREGSPLITNOEMPTY);

foreach($words as $word) { if(in_array($word, $badwords)) die($word.' cannot be in this field); }

That's it!

 
Comments
There aren't any comments here yet. Maybe you should add one!
Add a comment