Skip to content

Instantly share code, notes, and snippets.

@lcherone
Created May 3, 2014 20:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lcherone/55baab0de65c22e5fb06 to your computer and use it in GitHub Desktop.
Save lcherone/55baab0de65c22e5fb06 to your computer and use it in GitHub Desktop.
<?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