Instantly share code, notes, and snippets.

Embed
What would you like to do?
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