Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save A35G/2d98e723231345d6e550 to your computer and use it in GitHub Desktop.
Save A35G/2d98e723231345d6e550 to your computer and use it in GitHub Desktop.
Search and replace words (using random characters) contained in a text, using blacklists - Optimized version
<?php
/**
* Search and replace words with blacklist - v. 0.2
* ------------------------------------------------------------
* Optimized version - Thanks to my friend:
* Flavio 'darkjoker' Giobergia - https://github.com/darkjoker
* ------------------------------------------------------------
*
* The generation of random characters, has been eliminated, to make
* the script faster and more functional, and possibly to make the
* text fit in case of checksum on the text (for example: a file
* contained in an archive)
*
* ------------------------------------------------------------
*
* @param String $text_to_check - Text to be analyzed and eventually to be replace
* @param Array $blacklist_words - List of words to be find and replace
*/
$text_to_check = "Lorem ipsum DOLOR Bullshit";
$blacklist_words = array("God", "em", "shit", "pig");
function filtro($str) {
global $blacklist_words;
foreach ($blacklist_words as $w) {
$len = strlen($w);
$new = $w[0].str_repeat("*",$len-2).$w[$len-1];
$str = str_ireplace($w,$new,$str);
}
return $str;
}
echo filtro($text_to_check);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment