954,160 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple Banned Words Filter

The following script will take your variable $comments and filter out any bad words. eregi_replace is case-insensitive, so it will take out the word, no matter the way it is input.

[php]
$bad_words = explode('|', 'badword1|badword2|badword3|etc|etc');
foreach ($bad_words as $naughty)
{
$comments = eregi_replace($naughty, "#!@%*#", $comments);
}
[/php]

This will turn the input "I really think that you are a badword1, and a badword2"

into

"I really think that you are a #!@%*#, and a #!@%*#"

It will at least help keep a message board or guestbook clean.

himerus
Junior Poster in Training
84 posts since Aug 2003
Reputation Points: 11
Solved Threads: 0
 

Yeah, it's a good idea, but I think that just replacing the entire word with one character is neater so you can see the length of the character (and guess what it is if you really need to know what it was :p).

Gary King
PHP/vBulletin Guru
Team Colleague
417 posts since Nov 2003
Reputation Points: 53
Solved Threads: 5
 

Or people could also use my hack for vBulletin, which replaces censored text with an image instead - it helps visually :D

http://www.vbulletin.org/forum/showthread.php?s=&threadid=53665

Gary King
PHP/vBulletin Guru
Team Colleague
417 posts since Nov 2003
Reputation Points: 53
Solved Threads: 5
 

Thanks, Gary! :) You've got a lot of really good hacks over at vB.org

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

Thanks :)

Gary King
PHP/vBulletin Guru
Team Colleague
417 posts since Nov 2003
Reputation Points: 53
Solved Threads: 5
 

Watch out when filtering curse words. If the text submitted handles text formating like tags (or b wrapped in brackets like this and other forums do), I could easily do something shit and it would spell out you know what with the "s" in bold. I think this is a bug in vBulletin. I know I tried it on a few forums and it worked.

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

very nice

SMterminator
Light Poster
44 posts since Jan 2004
Reputation Points: 30
Solved Threads: 2
 
very nice


couldnt you just do striptags() and/or mysqlescapestring()
this should filter off tags, or you could custom code a bb code like parser so that it replaces , etc. with nothing.

extermin8tor
Newbie Poster
14 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You