Forum Moderators: coopster

Message Too Old, No Replies

Bad Word Filter help needed

Bad word filter needed

         

Aschx

2:36 am on Oct 3, 2007 (gmt 0)

10+ Year Member



Ok I need a bad word filter that I can manually type in the words to block and what to replace the words with. Ok so here is my post.php page script:

<?
mysql_connect("localhost","login name","pass");
mysql_select_db("database name");
$submit = $_POST['submit'];
$name = $_POST['name'];
$message = $_POST['message'];
if($submit){
$result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,message)"."VALUES ('NULL','$name','$message')");
} else {
}
?>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="200" height="300">
<param name="movie" value="shoutbox.swf">
<param name=quality value=high>
<embed src="shoutbox.swf" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="200" height="300"></embed>
</object>

eelixduppy

3:55 am on Oct 3, 2007 (gmt 0)



You can do something like this assuming you don't want to store the bad words in a flatfile or database table:

$bad_words = array('bw1','bw2','bw3','bw4');
$msg = [url=http://us2.php.net/manual/en/function.str-ireplace.php]str_ireplace[/url]($bad_words,"",$_POST['message']);

The function used above is for php5 only. If you need a case sensitive search and you don't have php 5 you could use preg_replace to get the same effect. Also, this only removes exact, case-insensitive matches of the bad word. If there are other characters in between the letters it will not remove them.

Hope this gives you a decent idea. Good luck and Welcome to WebmasterWorld! :)

Aschx

9:14 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



ok but I get this error:

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash

Aschx

11:58 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



dang it, sorry bout double post but, I really need help. With the code you gave me, it gives me the error above. Please help me out guys. Thanks!

eelixduppy

1:16 am on Oct 4, 2007 (gmt 0)



preg_replace has different syntax. I take it that you aren't running php 5.

Instead of making the whole string lowercase (because you will lose the case permanently) preg_replace is a pretty good option. It should look something like this:


$bad_words = array('/bw1/i','/bw2/i','/bw3/i','/bw4/i');
$msg = preg_replace($bad_words,'',$_POST['message']);

The 'i' modifier make its a case-sensitive search. good luck

Aschx

1:41 am on Oct 4, 2007 (gmt 0)

10+ Year Member



ok thanks if I have any more problems I will repost back