Tutorials PHP Filtering Content

Filtering Content

Today's tutorial will teach you how to filter strings. I'm a big supporter for freedom of speech but one man's joke is another man's insult. Sometimes you have to filter out offensive or dangerous content.

How do you filter content?

Well, you could do a str_replace for every word you want to filter, or you could put it all into an array.

$string = 'You suck at math. Even that retarded bully who always kicks your ass got a better grade.';
$parse  = array(
  'ass'    => 'butt',
  'suck'   => 'bad',
  'retarded' => 'special needs'
$search  = array_keys($parse);

// Produces "You bad at math. Even that special needs bully who always kicks your butt got a better grade."
$filtered = str_ireplace($search,$parse,$string);

You can replace anything you need to. You could filter out offensive material or keep your site age-appropriate. Now go back and filter out all the evil content on your site before you receive a call from a lawyer for harassment.

Posted by on . Category: PHP


No comments posted yet

You need to register or login to post new comments.