ProfanityFilter
ProfanityFilter copied to clipboard
Only replace bad word, not entire sentence that contains the bad word
I feel like I'm missing something. I simply want the 1 bad word replaced with ****, but instead the filter replaces the entire sentence with *****.
Thanks.
$check = new Check('../libs/wordlist.php');
$cleanWords = $check->obfuscateIfProfane($badWords);
echo $cleanWords;
Hi. Could you please provide a sentence this is happening with so I can test it against the code above. From memory it should only modify the word not the sentence so I'd like to fix it if possible. Also could you supply php version you are using.
Cheers
I confirmed that this is the case and I can replicate it. I will look to fix this soon, might be a bit harder than I originally thought.
Rgr doger.
Thanks.
I've managed to fix this on my script: function obfsucateProfaneWords($string) {
$profanities = array();
$profanityCount = count($this->profanities);
for ($i = 0; $i < $profanityCount; $i++) {
$profanities[ $i ] = $this->generateProfanityExpression(
$this->profanities[ $i ],
$this->characterExpressions,
$this->separatorExpression
);
}
foreach ($profanities as $profanity) {
if ($this->stringHasProfanity($string, $profanity)) {
preg_match($profanity,$string,$matches, PREG_OFFSET_CAPTURE);
foreach($matches as $match){
$str1 = substr($string,0,$match[1]);
$str3 = str_repeat("*",strlen($match[0]));
$str2 = substr($string,$match[1] + strlen($match[0]));
$string = $str1.$str3.$str2;
}
}
}
return $string;
}
It will also work with several instance of words since it works inside the foreach loop