Simple Swear Filter In PHP

30th September 2008

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.

  1. function filterwords($text){
  2. $filterWords = array('gosh', 'darn', 'poo');
  3. $filterCount = sizeof($filterWords);
  4. for($i=0; $i < $filterCount; $i++) {
  5. $text = preg_replace('/\b'.$filterWords[$i].'\b/ie',"str_repeat('*',strlen('$0'))",$text);
  6. }
  7. return $text;
  8. }

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().

  1. function filterwords($text){
  2. $filterWords = array('gosh', 'darn', 'poo');
  3. $filterCount = sizeof($filterWords);
  4. for ($i = 0; $i < $filterCount; $i++) {
  5. $text = preg_replace_callback('/\b' . $filterWords[$i] . '\b/i', function($matches){return str_repeat('*', strlen($matches[0]));}, $text);
  6. }
  7. return $text;
  8. }

 

Comments

Permalink
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.

philipnorton42 (Tue, 07/06/2010 - 08:55)

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

Michael Rich (Mon, 12/05/2011 - 03:04)

Permalink

Thanks for the tip Michael :)

philipnorton42 (Mon, 12/05/2011 - 09:09)

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 :)

steve17 (Fri, 04/25/2014 - 12:12)

Permalink
How would I just drop the swear word from the array instead of replacing it with asterisks?

Jim (Tue, 06/07/2016 - 17:56)

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 *)

hashbanganon (Sun, 03/05/2017 - 08:28)

Permalink
anyone managed to mod this code using preg_replace_callback ?

mike (Thu, 03/15/2018 - 21:07)

Permalink

anyone get this to work with preg_replace_callback for php 7?

nick (Thu, 04/16/2020 - 20:27)

Add new comment

The content of this field is kept private and will not be shown publicly.