PDA

View Full Version : PHP Code Snippet: Naughty Word Filter



Emancipator
06-30-2005, 06:57 AM
I built a word filter for my website. What it does is basically automatically build links out of celebrity names in my news. Example link http://www.moviesonline.ca/movienews_4634.html You can see all the celeb names are auto-linked. I built the celeb pages so I dont need to tell it what to link to, they follow a pattern so the array is very simple and no work on my part building it.

I realize this is of next to no value to anyone else but I have dumbed it down a bit and you can use it for simple things like a "bad words" filter. Keep in mind the variables are movie related cause well my CMS is for a movie site :P


<?
$cast = 'Profanity, Profanity2, Profanity3'; // Naughty Sailor Talk
$st_str = trim($cast, ' '); // Trim white spaces
$strs = explode(',', $cast); // Explode into an array by delimiter
$content = 'What the Profanity is going on' //the text to apply it to.

for($i=0; $i<sizeof($strs); $i++)
{
$str = ' '.$strs[$i].' ';
$text = eregi_replace($str,' ',$text);
}
?>

The way I use this is $cast is my cast list, and I replace it not with *** but with links to the cast bio. The $content is actually the field where i store the main content of my news in my, MYSQL database. By dumbing it down I have made it so you can easily build a bad word filter. May not be the best way to do it, but it works like a charm on my site.

Emancipator
06-30-2005, 08:02 AM
I updated the code with 2 changes I had on my site but forgot to share. Both are in the FOR loop. The first one is I added a check to make sure you are not pullling apart words. For example if you have *** in your filter and used the word assimilate the word would have been altered. With the new update it wont be.

Also str_replace is not a good way to replace words instead I used eregi_replace which is much better since it catches regardless of case.

piniyini
06-30-2005, 11:55 AM
Believe it or not I was looking for something like this, so thanks

Emancipator
06-30-2005, 12:38 PM
let me know if you have any questions about it.. I made it for something else and dumbed it down to just be a profanity filter figuring somebody might use it. I will likely enhance it if anyone has any thoughts on it.

James
06-30-2005, 03:35 PM
That looks pretty cool. I might use it if Google whines at me about how much I swear on my Blog or any of my sites; the potty mouth is a fowl desease which I'm too lazy to cure...

moonshield
06-30-2005, 06:26 PM
a fowl disease eh?


Neat little snippet, I will use it I will.

Emancipator
06-30-2005, 08:18 PM
always nice to do up a snippet others can use...

mrosen
05-28-2009, 08:10 PM
I was able to easily integrate the webpurify profanity filter (http://www.webpurify.com) into my php application. I decided to use it when writing my own filter was getting a little to complicated.

abusinessfinder
07-28-2009, 11:32 PM
This is great. I built something similar, but with the addition of notifying the webmaster / moderator when swearing occurs (to ban an account, etc.). I think it's funny what you did with the celebrity names though, haha.