PDA

View Full Version : PHP CODE : Word Filter Script Example



Emancipator
10-06-2005, 11:57 AM
I figured I would share some code. Its been awhile since I have and this code is something I wrote in a few minutes to use on my dvd site. It is just a filter however you can use it quite creatively to have some very interesting results.

In my case I am using a modified variation of it to filter specific words out so I can generate meta data, and also links to amazon without an aws feed.

Example Application: I use it to help determine what to link to on amazon from my news with a filter algorithm based on the content of the page. http://www.avixion.ca/news.php?id=5767 bottom just about the google adsense.

The function itself


function filterwords($text)
{
$word = "words you want to filter";
$strs = explode(' ', $word);
$text = ' '.$text.' ';
for($i=0; $i<sizeof($strs); $i++)
{
$str = ' '.$strs[$i].' ';
$text = eregi_replace($str,' ',$text);
}
return $text;
}

To use it is easy, just put the function into an inclusion file or simply put it at the top of your php page. Call it like this <?php echo filterwords(what_to_filter); ?>

Perfect? No? There is a hundred different ways you could do it better. In the same token if you wanted to turn this into a profanity filter you could in seconds. Example, put the profane words where in the $cast, and then replace $text = eregi_replace($str,' ',$text); with $text = eregi_replace($str,'******',$text); It will replace the profanity with ***

Hopefully this helps somebody. I reccomend putting this in an inclusion file so you can easily update in seconds any pages calling the function.