Created
May 22, 2014 13:47
-
-
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
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 | |
/** | |
* 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