Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

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

by haz on Sunday, July 12th 2009 at 06:33 AM

Please forgive me but my background is in mainframes. Before i go down this road would this script possibly be able to filter language from video running via a PowerDVD application on the PC (xp windows)?

by James Rosenstein on Sunday, August 23rd 2009 at 08:30 PM

I prefer to just use a web service like <a href="http://www.webpurify.com">webpurify.com</a> to filter out foul language or profanity.

by ripepixel on Tuesday, October 13th 2009 at 09:10 AM

I'm taking the words from a database, instead of a file, however, it is only reading the first value. I've tried with a file and still the same thing.
Any ideas?

foreach($qry->result_array() as $word) {
$words[] = $word['word'];
}
foreach($words as $foulWord) {
$foulWord = trim($foulWord);
... etc

by bob on Wednesday, February 3rd 2010 at 05:01 PM

does it work on PC videogames (call of duty, Battlefield)

by bob on Wednesday, February 3rd 2010 at 05:01 PM

does it work on PC videogames (call of duty, Battlefield)


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 >>
Sponsors
Discover Geekpedia

Other Resources