ProfanityFilter icon indicating copy to clipboard operation
ProfanityFilter copied to clipboard

Only replace bad word, not entire sentence that contains the bad word

Open EddieLongStockings opened this issue 7 years ago • 4 comments

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;

EddieLongStockings avatar Apr 30 '17 02:04 EddieLongStockings

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

developerdino avatar Apr 30 '17 03:04 developerdino

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.

developerdino avatar Apr 30 '17 13:04 developerdino

Rgr doger.

Thanks.

EddieLongStockings avatar May 01 '17 18:05 EddieLongStockings

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

King47 avatar Jun 17 '18 11:06 King47