Use the following function to filter out words from user input. It works by having a pre-set array of words that are to be excluded, this array is then looped through and each item is used to replace any instances of that word within the text. The regular expression uses the \b character class, which stands for any word boundary. This way you don't get the middle of words being filtered out when they are not meant to be.
By using the e of the preg_replace function it is possible to run PHP functions within the output. In this case we count the number of characters found in the replace and use this to create a string of stars (*) of equal length.
function filterwords($text){
$filterWords = array('gosh', 'darn', 'poo');
$filterCount = sizeof($filterWords);
for($i=0; $i < $filterCount; $i++) {
$text = preg_replace('/\b'.$filterWords[$i].'\b/ie',"str_repeat('*',strlen('$0'))",$text);
}
return $text;
}
When the following text is run through this function.
echo filterwords('Darn, I have a mild form of tourettes, poo!');
It produces the following result.
****, I have a mild form of tourettes, ***!
Update: Because a few people have asked I have updated this example using preg_replace_callback().
function filterwords($text){
$filterWords = array('gosh', 'darn', 'poo');
$filterCount = sizeof($filterWords);
for ($i = 0; $i < $filterCount; $i++) {
$text = preg_replace_callback('/\b' . $filterWords[$i] . '\b/i', function($matches){return str_repeat('*', strlen($matches[0]));}, $text);
}
return $text;
}
Comments
Hey, really nice script, I can tell you've taken your time on it.
I was dealing with a word dictionary called badwords.txt I found on Google Code which had some words in like 'sh!t'. The played havok with your script until I escaped the charachters:
$text = preg_replace('/\b'.preg_quote($filterWords[$i]).'\b/ie',"str_repeat('*',strlen('$0'))",$text);
I agree, WebPurify is a step too far.
Thanks for the tip Michael :)
anyone get this to work with preg_replace_callback for php 7?