Created
June 4, 2013 19:32
-
-
Save dcblogdev/5708821 to your computer and use it in GitHub Desktop.
detecting and replacing bad words in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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