Jump to content

[SOLVED] bad word filter and Mysql database


irkevin

Recommended Posts

Hi,

 

I've been searching for hours for a solution and now i'm stuck. I have this code

 

function filterBadWords($str)
{
    $badwords = array("barwords_goes_here","barwords_goes_here","barwords_goes_here");
    $replacements = "*";
    
    foreach ($badwords as $badword)
    {
          $str = eregi_replace($badword, str_repeat('*', strlen($badword)), $str);
    }  
    
    return $str;
}  

 

I created a mysql table, and store the bad words there.

 

My question is : How do I replace the 'barwords_goes_here', with the data in the mysql table?

Link to comment
Share on other sites

im not sure there must be a better way i think but i would do something like

function filterBadWords($str)
{
    $result = mysql_query("SELECT `word` FROM `badwords`") or die(mysql_error()); 
    $replacements = "*";
    
    while($row = mysql_fetch_assoc($result) != null)
    {
          $str = eregi_replace($row['word'], str_repeat('*', strlen($badword)), $str);
    }  
    
    return $str;
}

but like i say i am sure there would be a better mysql way but i dont know it sorry

 

Scott.

 

Link to comment
Share on other sites

Hey, its working  ;D it did what i expected. Thanks for your help

 

the complete code. Had to change strlen($badword) to strlen($row['words'])

<?php
function filterBadWords($str)
{
include("../config.php");

    $result = mysql_query("SELECT words FROM badword") or die(mysql_error()); 
    $replacements = "*";
    
    while($row = mysql_fetch_assoc($result))
    {
          $str = eregi_replace($row['words'], str_repeat('*', strlen($row['words'])), $str);
    }  
    
    return $str;
}

echo filterBadWords("your_badword_from_database");
?> 

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.