Jump to content

Replacing values from MySQL


areeb
 Share

Recommended Posts

Hello,I have a Shoutbox, and I want to have a bad word censer. i.e. the bad words should be automatically replaced with the words specified.Currently I am using Badwords insides the PHP File. I am using this.

// Defining an array$badword_array['badword1'] = 'replace1';$badword_array['badword2'] = 'replace2';$badword_array['badword3'] = 'replace3';// And using this, to Replace Bad Words from the Text i.e $messageforeach($badword_array as $insult=>$ok){        $message = eregi_replace("$insult", "$ok", "$message");        }

However, if we need to edit the Badwords etc, then we need to edit the PHP File itself. :)Can we have the possibility that, we have a MySQL Table having two feilds i.e. badword and replace.e.g.badword / replacebadword1 / replace1badword2 / replace2and so on .....The PHP Script should look for the bad word in the "badword" feild, and if anu bad word occur then it shoukd use appropriate replacement, as mentioned in corresponding "replace" Tables :)Please Help.

Link to comment
Share on other sites

You could do that fairly easily, actually...just create a table with an ID field, 'badword' field, and a 'replace' field. Then just write a function in PHP that pull s the badword/replacements from the database and search the string you're testing for said words and replace them. For example:

//Retrieves bad words from the database. Call this at the top of your php code and//Reuse the array afterwards to save timefunction get_bad words(){$Result = mysql_query("SELECT badword, replace FROM wordstable");$Array = array();for($i = 0; $i < mysql_numrows($Result); $i++)    {        $key = mysql_result($Result, 'badword', $i);        $value = mysql_result($Result, 'replace' $i);        $array[$key] = $value;    }    return $Array;}//Given an associative array of bad words and replacements, this function replaces//All instances of those wordsfunction replace_bad_words($test, $badwords){    $badkeys = array_keys($badwords);      for each($badkeys as $thisbadword)    {        str_replace($thisbadword, $badkeys[$thisbadword], $test);    }    return $test;}

Link to comment
Share on other sites

Thanks a lot Sir for writing out this Code ..But actually, I am new to PHP :)If I have a Text in the variable named $text, lets say,$text = "This is a badword which has to be replaced";then how can we use functions get_bad_words() & replace_bad_words() to replace the bad words ...I have gone through ur explanations and have understood what exactly have been done .. but how to use the fuinction :)

Link to comment
Share on other sites

Actually, you just need to use your own code, but instead of defining the array, you define it with the gotten data from your database :)

// Defining an array$badword_array = array();$badwords = mysql_query("SELECT * FROM Badwords ORDER BY Badwords");while ($row = mysql_fetch_array($badwords,MYSQL_ASSOC)){ $badword_array["{$row['Badword']}"] = $row['Replacement'];}// And using this, to Replace Bad Words from the Text i.e $messageforeach($badword_array as $insult=>$ok){       $message = eregi_replace("$insult", "$ok", "$message");       }
But this can even still be shorter. Creating an array is not necessary, while we can also replace all the words in each array fetch of the database result instantly, without firstly putting the data in an array :)So I guess this should also work:
// NOT Defining an array// And using this, to Replace Bad Words from the Text i.e $message$badwords = mysql_query("SELECT * FROM Badwords ORDER BY Badwords");while ($row = mysql_fetch_array($badwords,MYSQL_ASSOC)){ $message = eregi_replace($row['Badword'], $row['Replacement'], $message);}
(I used a while loop, but a foreach can also be used, in conjunction with mysql_num_rows()) Edited by Dan The Prof
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...