Jump to content

[SOLVED] bad word filter


ball420

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.