Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
EddieLongStockings opened this issue Apr 30, 2017 · 4 comments

Comments

@EddieLongStockings
Copy link

EddieLongStockings commented Apr 30, 2017

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

Rgr doger.

Thanks.

@King47
Copy link

King47 commented Jun 17, 2018

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

@developerdino developerdino moved this from To do to Incoming in Profanity Filter Version 2 Jul 11, 2020
@developerdino developerdino moved this from Triage to Accepted in Profanity Filter Version 2 Jul 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

3 participants