Geekpedia Programming Tutorials






Foul language filter in PHP

You will learn how to create a simple PHP foul language filter function which uses a file containing foul words to replace them in the string of your choice with asterisks.

On Saturday, February 11th 2006 at 07:44 AM
By Andrew Pociu (View Profile)
****-   (Rated 3.3 with 6 votes)
Contextual Ads
More PHP Resources
Advertisement
In this tutorial we will create a simple PHP function which will replace foul words with asterisks or any character or string of your choice. The list of foul words will be retrieved from a text file. Each foul word must be entered on a different line. A sample foul language file can be download at http://www.geekpedia.com/Scripts/foul.txt (reader's discretion is advised).



Let's view the function. It's not too complicated and the comments should guide you through:





function LangFilter($ToFilter)

{

   // Open the foul.txt for extracting the foul words

   $Foul = @file("foul.txt");

   // Loop through the foul words

   foreach ($Foul as $FoulWord)

   {

      // Store the current foul word in a variable

      $FoulWord = trim($FoulWord);

      // If there's a match for the fould world inside the string

      if (preg_match("/".$FoulWord."/i", $ToFilter))

      {

         // Get the word length, so that we know how many characters

         // we need to replace with asterisks


         $WordLength = strlen($FoulWord);

         // Loop through the word's characters

         for ($i = 1; $i <= $WordLength; $i++)

         {

            // Replace the characters of the foul word with * (asterisks)

            $RepChar .= "*";

         }

         // Replace the dirty string with the filtered string

         $ToFilter = eregi_replace($FoulWord, $RepChar, trim($ToFilter));

         $RepChar = "";

      }

   }

   // Return the new string


   return $ToFilter;

}



All you really need to do for this script to work, is to place the foul.txt file in the same directory, and to call the LangFilter() function on the string that you need filtered, like in the example below:





echo LangFilter("If dirtbag would have been a dirty word, it would be replaced now by asterisks.");



Indeed, if dirtbag would've been on the list, the following line would output:
If ******* would have been a dirty word, it would be replaced now by asterisks.




Here is the same function but without the comments, in case you want to copy and paste it in your code, and you're not interested in the comments.





function LangFilter($ToFilter)

{

   $Foul = @file("foul.txt");


   foreach ($Foul as $FoulWord)

   {

      $FoulWord = trim($FoulWord);


      if (preg_match("/".$FoulWord."/i", $ToFilter))

      {

         $WordLength = strlen($FoulWord);


         for ($i = 1; $i <= $WordLength; $i++)

         {

            $RepChar .= "*";

         }

         $ToFilter = eregi_replace($FoulWord, $RepChar, trim($ToFilter));

         $RepChar = "";

      }

   }


   return $ToFilter;

}
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Tejal on Wednesday, April 5th 2006 at 08:14 AM

Hi,

When i run unlink($filepath) on localhost i am getting below error

Warning: unlink(E: Document (2).txt) [function.unlink]: Permission denied in C:\Inetpub\wwwroot\Mapmyoffer\main.php on line

Can you help me?

by Andrei Pociu on Wednesday, April 5th 2006 at 08:45 AM

I'm not sure what that has to do with this tutorial, so you should ask in the forums next time.
Your problem is that the IIS user doesn't have permission to delete that path. You need to add the necessary permissions.

by Nikita Smirnov on Sunday, April 16th 2006 at 01:41 PM

The tutorial is OK, but could be done more effeciently:
1) Using str_replace instead of eregi_replace.
2) Instead of looping and adding the stars, do $RepChar = str_repeat('*', strlen($FoulWord));

by Joe on Saturday, June 3rd 2006 at 10:45 PM

It doesn't work at all.

by gfd on Tuesday, June 13th 2006 at 09:32 AM

TO TEJAL:

chmod you directory to 0777

by rasta on Monday, July 31st 2006 at 09:17 PM

Good tutorial thanks

by Dave Roberts on Sunday, December 2nd 2007 at 11:23 AM

First, thank you for creating and sharing this script! It works absolutely fantastic!

Second, I configured the script a little differently and thought I would share it with everyone.

1. I placed the script in a folder called \"commonfiles\". This is where I keep all scripts I use on a variety of marketing-specific microsites.

2. I placed the following string of code inside my php-based form processing script:

if(!preg_match(\"/LangFilter()/\",$_POST[\"Company\"])) {die(\"<p align=\'center\'><font face=\'Arial\' size=\'3\' color=\'#000000\'>You have entered inappropriate language in one of the fields provided on this form. <br><br>You will need to click on your browsers \\\"Back\\\" button and correct this unacceptable entry in order to complete the submission process...</font></p>\");}

Cheers!

by Erez on Friday, March 6th 2009 at 06:39 AM

rsort($Foul);
So included words won't come before


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs PHP Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons