Created
May 3, 2014 20:02
-
-
Save lcherone/55baab0de65c22e5fb06 to your computer and use it in GitHub Desktop.
swear_filter function for https://gist.github.com/lcherone/6f8043447f7f0099d430
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 | |
/** | |
* Swear word filtering function, requiers a list of words, | |
* Second parameter reveals *n letters | |
* | |
* @wordlist https://gist.github.com/lcherone/6f8043447f7f0099d430 | |
* | |
* @param string $str | |
* @param int $reveal | |
* @return string | |
*/ | |
function swear_filter($str, $reveal=null) { | |
//load words | |
$words = join("|", array_filter(array_map('preg_quote',array_map('trim', file('/path/to/badwords.txt'))))); | |
if($reveal !=null && is_numeric($reveal)){ | |
return preg_replace("/\b($words)\b/uie", '"".substr("$1",0,'.$reveal.').str_repeat("*",strlen("$1")-'.$reveal.').""', $str); | |
}else{ | |
return preg_replace("/\b($words)\b/uie", '"".str_repeat("*",strlen("$1")).""', $str); | |
} | |
} | |
echo swear_filter('Use your imagination.', 1); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment