Posted by tim in I hate technology. on December 7, 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 = preg_split('/\s+/', $input, -1, PREG_SPLIT_NO_EMPTY);

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

That's it!

  • Add a comment!
Copyright © 2024 SkuddBlog