Skip to content

Instantly share code, notes, and snippets.

@A35G
Created May 20, 2014 14:46
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/19446be9b636999117f4 to your computer and use it in GitHub Desktop.
Save A35G/19446be9b636999117f4 to your computer and use it in GitHub Desktop.
Search and replace words (using random characters) contained in a text, using blacklists
<?php
// Text to filter and to censure
$text_to_check = "Lorem ipsum DOLOR Bullshit";
// List of words to censor
$blacklist_words = array("God", "em", "shit", "pig");
//$blacklist_words = "shit";
// Function used to generate a random string length as word to replace
function generateString($length) {
$alph_char = array('33', '35', '36', '37', '38', '64');
$resString = '';
for ($i = 0; $i < $length; $i++)
$resString .= chr($alph_char[array_rand($alph_char, 1)]);
return $resString;
}
// Function to analyze text and cancel words length shorter than 3 (three) characters
function analyzePhrase($phrase) {
for ($plo = 0; $plo <= count($phrase); $plo++) {
if (strlen($phrase[$plo]) <= 3) {
if (!is_numeric($phrase[$plo]))
unset($phrase[$plo]);
}
}
return count($phrase);
}
function censorshipWords($text_w, $censored) {
$in_phrase = $text_w;
if (isset($censored) && !empty($censored)){
if (is_array($censored)) {
for ($t = 0; $t < count($censored); $t++)
$in_phrase = str_ireplace($censored[$t], generateString(strlen($censored[$t])), $in_phrase);
} else {
$in_phrase = str_ireplace($censored, generateString(strlen($censored)), $text_w);
}
} else {
$in_phrase = $text_w;
}
return $in_phrase;
}
$the_phrase = explode(" ", htmlentities($text_to_check));
$rm_filter = analyzePhrase($the_phrase);
if (!empty($rm_filter))
echo censorshipWords($text_to_check, $blacklist_words);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment