Skip to content

Instantly share code, notes, and snippets.

@dcblogdev
Created June 4, 2013 19:32
Show Gist options
  • Save dcblogdev/5708821 to your computer and use it in GitHub Desktop.
Save dcblogdev/5708821 to your computer and use it in GitHub Desktop.
detecting and replacing bad words in a string
<?php
$text = 'word1 some more words. word2 and some more words';
$text = preg_replace_callback('!\w+!', 'filter_bad_words', $text);
echo $text;
function filter_bad_words($matches) {
$bad_words = array(
'word1' => 'gosh',
'word2' => 'darn',
);
$replace = $bad_words[$matches[0]];
return isset($replace) ? $replace : $matches[0];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment