developerdino / ProfanityFilter

A simple PHP class to test if a string has a profanity in it.
70 stars 27 forks source link

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

Open EddieLongStockings opened 7 years ago

EddieLongStockings commented 7 years ago

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;
developerdino commented 7 years ago

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 commented 7 years ago

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.

EddieLongStockings commented 7 years ago

Rgr doger.

Thanks.

King47 commented 6 years ago

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