It's free and you have access to premium codes!
Welcome back! Please login to your account.
Don't worry, we'll send you a message to help you to recover your acount.
Please check your email for instructions to activate your account.
Written by 4 November 2022
Spambots, bad users will leave some profane words in your user-defined content like comments. This bad words filter class uses a predefined list of bad words to filter out bad words visually. Bad words will show its first letter followed by ***** for remainder of the number of letters.
<!-- this script is provided by https://www.phpfreecode.com coded by: Kerixa Inc. -->
<?php
class BadWords_Funcs{
public static $badWords = [];
static function __constructStatic(){
$fp = fopen("https://www.cs.cmu.edu/~biglou/resources/bad-words.txt", "r");
if($fp){
while(($line = fgets($fp)) !== false){
array_push(self::$badWords, trim($line, "\t\n\r"));
}
if(!feof($fp)){
echo "Unexpected fgets() error encountered.";
}
fclose($fp);
}
}
static function get_badWords(){
return self::$badWords;
}
static function filteredString($sentence){
$pattern = ".+*^$\(\)\[\]\?&@#'\"! \n\t\r";
$sentencedArray = preg_split("/([".$pattern."])/", $sentence, -1, PREG_SPLIT_DELIM_CAPTURE);
$newArray = [];
for($i = 0; $i < count($sentencedArray); $i++){
$word = $sentencedArray[$i];
$filtered_word = $word;
for($j = 0; $j < count(self::$badWords); $j++){
// bad-word detected, filter and break inner loop for next bad-word
$badword = self::$badWords[$j];
if(strcasecmp(strtolower($badword), strtolower($word)) === 0){
$badword_length = strlen($badword);
$firstLetter = substr($word, 0, 1);
$filtered_word = "";
$filtered_word .= $firstLetter;
// short bad-word
if($badword_length < 3){
for($k = 0; $k < $badword_length - 1; $k++){
$filtered_word .= "*";
}
}
// long bad-word
else{
$lastLetter = substr($word, -1);
for($k = 0; $k < $badword_length - 2; $k++){
$filtered_word .= "*";
}
$filtered_word .= $lastLetter;
}
break;
}
}
array_push($newArray, $filtered_word);
}
return implode("", $newArray);
}
}
?><a target='_blank' href='https://www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>
Comments
Here you can leave us commments. Let us know what you think about this code tutorial!