Simple Swear Filter In PHP

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

True, although the solution in this post is completely free, whereas the WebPurify seems expensive for a very simple (and not all that important) web service.
Name
Philip Norton
Permalink

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 's***'. 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.

Permalink

Thanks for the tip Michael :)

Name
Philip Norton
Permalink
Hey Philip this is the only script that has worked as advertized! Thanks for that. Now I have to sit back and work out how and why it works :)
Permalink
How would I just drop the swear word from the array instead of replacing it with asterisks?
Permalink
Quickest is to replace $text = preg_replace('/\b'.$filterWords[$i].'\b/ie',"str_repeat('*',strlen('$0'))",$text); with $text = preg_replace('/\b'.$filterWords[$i].'\b/ie',"str_repeat('',strlen('$0'))",$text); (removed the *)
Permalink
anyone managed to mod this code using preg_replace_callback ?
Permalink

anyone get this to work with preg_replace_callback for php 7?

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.