Im trying to make a naughty word filter. It removes bad words fine, but instances where there is a bad word found in the text like "assist" and "asses" get caught in the filter as well. Strangely though if the sentence is: My asses to assist me." the clean version will read: My asses to ***ist me." It seems to clear the first use of the word in another word, but then blocks the rest. Any ideas? My script is below. Thanks.
function clean($wordlist,$value)
{
return preg_replace("/($wordlist)/i", '***',trim($value));
}
?>
Output:
Quote:
Words: stone twoot three fourth Cleaned: st*** ***ot *** ***th
Words: under oven tree Cleaned: under oven tree
Words: cookie oneder ream Cleaned: cookie ***der ream
With a lot of bad words, this function can get consuming, but it does filter whole words only, and will not split words up.
Using wirehoppers example, with a litte changing around.
PHP Code:
<?php /* Read in from the file here, not in the function - you only need to read the file once */ $wordlist[] = 'one'; $wordlist[] = 'two'; $wordlist[] = 'three'; $wordlist[] = 'four';
/* Sample data */ $words = 'stone twoot three fourth under oven tree cookie oneder ream one sixone threetwo fournine four';
foreach ($wordlist as $v) $words = clean($v,$words);
function clean($wordlist,$value) { return preg_replace("/\b$wordlist\b/i", '***',trim($value)); }
echo 'Words: '.$words.PHP_EOL; ?>
Code:
Words: stone twoot *** fourth under oven tree cookie oneder ream *** sixone threetwo fournine ***
i ended up finding that the word "a.s.s." was in my list. I think the dots were messing up the expression. For thos interested, this is my new code. Thanks for any suggestions to get it where it is.
Code:
$_SESSION[wordlist] = join("|", array_map('trim', file('standards/badwords.txt')));
function cleanWords($value) {
global $_SESSION;
$value = preg_replace("/\b($_SESSION[wordlist])\b/ie", 'str_repeat("*", strlen("\\1")) ', $value);
return $value;
}
The Following User Says Thank You to cesarcesar For This Useful Post: