ball420 Posted June 20, 2007 Share Posted June 20, 2007 i can't get this bad word filter to work. I know it's simple am i missing somthing? thanks for your help. <?php $host= 'localhost'; $username= 'root'; $password= ''; $db_name= 'guest'; $tbl_name= 'book'; function filter($string) { $pattern[0] = 'fuck'; $pattern[1] = 'ass'; $pattern[2] = 'shit'; $replacement[0] = 'beep'; $replacement[1] = 'beep'; $replacement[2] = 'beep'; return preg_replace($pattern, $replacement, $string); } // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect server"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ ?> <tr> <td> <table width="600" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <!--DWLayoutTable--> <tr> <td height="26" colspan="3" valign="top"><div class="topbar"><span class="style1">......</span><strong>Posted on</strong> <span class="style1">...</span><strong><?php echo $rows['datetime']; ?></strong><span class="style1">.....</span><strong>I give it<span class="style1">.</span> <?php echo $rows['rate']; ?> </strong> </span><strong>out of 5 </strong></div></td> </tr> <tr> <td width="89" height="25" valign="top">Name</td> <td width="9" valign="top">:</td> <td width="480" valign="top"><?php echo $rows['name']; ?></td> </tr> <tr> <td height="25" valign="top">Subject</td> <td valign="top">:</td> <td valign="top"><?php echo $rows['subject']; ?></td> </tr> <tr> <td height="25" valign="top">Comment</td> <td valign="top">:</td> <td valign="top"><?php echo $rows['comment']; ?></td> </tr> </table> <?php } mysql_close(); //close database ?> </p> <p> </p> <p><a name="formfill"></a> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <!--DWLayoutTable--> <tr> <form id="form1" name="form1" method="post" action="addguestbook.php"> <td width="400" height="237" valign="top"> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <!--DWLayoutTable--> <tr> <td height="44" colspan="6" valign="top"><p><strong>Fill out all the info below to post your comments. We do not post or distribute any email addresses. </strong></p> </td> </tr> <tr> <td width="97" height="2"></td> <td width="3"></td> <td colspan="3" rowspan="2" valign="top"><input name="name" type="text" id="name" size="40" /></td> <td width="5"></td> </tr> <tr> <td height="25" valign="top">Name</td> <td valign="top">:</td> <td></td> </tr> <tr> <td height="2"></td> <td rowspan="2" valign="top">:</td> <td colspan="3" rowspan="2" valign="top"><input name="email" type="text" id="email" size="40" /></td> <td></td> </tr> <tr> <td height="25" valign="top">Email</td> <td></td> </tr> <tr> <td height="2"></td> <td></td> <td width="2"></td> <td width="150"></td> <td width="100" rowspan="2" valign="top"><label> <input name="rate" type="text" id="rate" size="1"> </label></td> <td></td> </tr> <tr> <td height="25" colspan="4" valign="top"><p>Your rating 1 to 5 as 5 being the highest </p></td> <td></td> </tr> <tr> <td height="28" valign="top">Subject</td> <td valign="top">:</td> <td colspan="3" valign="top"><label> <input name="subject" type="text" size="40" /> </label></td> <td></td> </tr> <tr> <td height="13"></td> <td></td> <td colspan="3" rowspan="3" valign="top"><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td> <td></td> </tr> <tr> <td height="30" valign="top">Comment</td> <td valign="top">:</td> <td></td> </tr> <tr> <td height="12"></td> <td></td> <td></td> </tr> <tr> <td height="30"></td> <td></td> <td> </td> <td colspan="2" valign="top"><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td> <td></td> </tr> </table></td> </form> </tr> </table> </p> > </body> Link to comment https://forums.phpfreaks.com/topic/56404-solved-bad-word-filter/ Share on other sites More sharing options...
taith Posted June 20, 2007 Share Posted June 20, 2007 ..? your not requesting that function anywhere...? Link to comment https://forums.phpfreaks.com/topic/56404-solved-bad-word-filter/#findComment-278560 Share on other sites More sharing options...
chigley Posted June 20, 2007 Share Posted June 20, 2007 <?php while($rows=mysql_fetch_array($result)){ foreach($rows as $k => $v) { $rows[$k] = filter($v); } ?> <tr> <td> <!-- Continue your code here --> Link to comment https://forums.phpfreaks.com/topic/56404-solved-bad-word-filter/#findComment-278565 Share on other sites More sharing options...
rea|and Posted June 20, 2007 Share Posted June 20, 2007 Try to change your function in this way: function filter($string) { $pattern[0] = 'fuck'; $pattern[1] = 'ass'; $pattern[2] = 'shit'; $replacement = 'beep'; $pattern=implode('|',$pattern); #matches only whole words return preg_replace("/\b($pattern)\b/i", $replacement, $string); } Link to comment https://forums.phpfreaks.com/topic/56404-solved-bad-word-filter/#findComment-278571 Share on other sites More sharing options...
ball420 Posted June 20, 2007 Author Share Posted June 20, 2007 i tried both examples still with no success. thanks for all the help Link to comment https://forums.phpfreaks.com/topic/56404-solved-bad-word-filter/#findComment-278577 Share on other sites More sharing options...
taith Posted June 20, 2007 Share Posted June 20, 2007 heres the one i use... <?php $badwords=array(); function filter_censor($string){ global $badwords; if(empty($badwords)){ $result=@mysql_query("SELECT `words` FROM `badwords`"); while($row=mysql_fetch_array($result)) $badwords[]=$row[words]; } foreach($badwords as $badword) $string=str_ireplace($badword, str_pad("",strlen($badword),"*"), $string); return $string; } ?> Link to comment https://forums.phpfreaks.com/topic/56404-solved-bad-word-filter/#findComment-278582 Share on other sites More sharing options...
chigley Posted June 20, 2007 Share Posted June 20, 2007 Well as long as your function works, my solution will work. Perhaps use this instead: <?php function filter($string, $replace) { $banned = array("fuck", "ass", "shit"); foreach($banned as $word) { $string = str_replace($word, $replace, $string); } return $string; } $host= 'localhost'; $username= 'root'; $password= ''; $db_name= 'guest'; $tbl_name= 'book'; // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect server"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); while($rows = mysql_fetch_array($result)){ foreach($rows as $key => $value) { $rows[$key] = filter($value, "beep"); } ?> <tr> <td> <table width="600" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <!--DWLayoutTable--> <tr> <td height="26" colspan="3" valign="top"><div class="topbar"><span class="style1">......</span><strong>Posted on</strong> <span class="style1">...</span><strong><?php echo $rows['datetime']; ?></strong><span class="style1">.....</span><strong>I give it<span class="style1">.</span> <?php echo $rows['rate']; ?> </strong> </span><strong>out of 5 </strong></div></td> </tr> <tr> <td width="89" height="25" valign="top">Name</td> <td width="9" valign="top">:</td> <td width="480" valign="top"><?php echo $rows['name']; ?></td> </tr> <tr> <td height="25" valign="top">Subject</td> <td valign="top">:</td> <td valign="top"><?php echo $rows['subject']; ?></td> </tr> <tr> <td height="25" valign="top">Comment</td> <td valign="top">:</td> <td valign="top"><?php echo $rows['comment']; ?></td> </tr> </table> <?php } mysql_close(); //close database ?> </p> <p> </p> <p><a name="formfill"></a> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <!--DWLayoutTable--> <tr> <form id="form1" name="form1" method="post" action="addguestbook.php"> <td width="400" height="237" valign="top"> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <!--DWLayoutTable--> <tr> <td height="44" colspan="6" valign="top"><p><strong>Fill out all the info below to post your comments. We do not post or distribute any email addresses. </strong></p> </td> </tr> <tr> <td width="97" height="2"></td> <td width="3"></td> <td colspan="3" rowspan="2" valign="top"><input name="name" type="text" id="name" size="40" /></td> <td width="5"></td> </tr> <tr> <td height="25" valign="top">Name</td> <td valign="top">:</td> <td></td> </tr> <tr> <td height="2"></td> <td rowspan="2" valign="top">:</td> <td colspan="3" rowspan="2" valign="top"><input name="email" type="text" id="email" size="40" /></td> <td></td> </tr> <tr> <td height="25" valign="top">Email</td> <td></td> </tr> <tr> <td height="2"></td> <td></td> <td width="2"></td> <td width="150"></td> <td width="100" rowspan="2" valign="top"><label> <input name="rate" type="text" id="rate" size="1"> </label></td> <td></td> </tr> <tr> <td height="25" colspan="4" valign="top"><p>Your rating 1 to 5 as 5 being the highest </p></td> <td></td> </tr> <tr> <td height="28" valign="top">Subject</td> <td valign="top">:</td> <td colspan="3" valign="top"><label> <input name="subject" type="text" size="40" /> </label></td> <td></td> </tr> <tr> <td height="13"></td> <td></td> <td colspan="3" rowspan="3" valign="top"><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td> <td></td> </tr> <tr> <td height="30" valign="top">Comment</td> <td valign="top">:</td> <td></td> </tr> <tr> <td height="12"></td> <td></td> <td></td> </tr> <tr> <td height="30"></td> <td></td> <td> </td> <td colspan="2" valign="top"><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td> <td></td> </tr> </table></td> </form> </tr> </table> </p> > </body> Link to comment https://forums.phpfreaks.com/topic/56404-solved-bad-word-filter/#findComment-278585 Share on other sites More sharing options...
ball420 Posted June 20, 2007 Author Share Posted June 20, 2007 thanks for all your help topic soved both solutions work thanks a ton!!phpfreaks is the spot!!! Link to comment https://forums.phpfreaks.com/topic/56404-solved-bad-word-filter/#findComment-278594 Share on other sites More sharing options...
taith Posted June 20, 2007 Share Posted June 20, 2007 make sure thats str_ireplace()... so its not case sensitive... Link to comment https://forums.phpfreaks.com/topic/56404-solved-bad-word-filter/#findComment-278595 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.