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?

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.

 

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");
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.