papaface Posted February 24, 2007 Share Posted February 24, 2007 Hello, How can I put the results from the db: $selectbadwords = mysql_query("select bad_word,bad_replace from filter where `type`='bad'"); in an array like this: $bad_words = array("badword1fromdb","badword2fromdb","badword3fromdb","badword4fromdb") My memory seems to have gone these last few days lol Quote Link to comment Share on other sites More sharing options...
paul2463 Posted February 24, 2007 Share Posted February 24, 2007 $selectbadwords = mysql_query("select bad_word,bad_replace from filter where `type`='bad'"); while($row = mysql_fetch_assoc($selectbadwords)) { $bad_words[] = $row['bad_word']; } Quote Link to comment Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 Thats what I thought it was. Im trying to put it into this bad word filter. But it doesnt quite work the way i want. This is my code $str = "hello i wish you would crap off!"; $selectbadwords = mysql_query("select filter_word,filter_replace from filter where `type`='bad'"); while ($row = mysql_fetch_assoc($selectbadwords)) { $badword[] = $row["filter_word"]; $replacement[] = $row["filter_replace"]; } foreach ($badword AS $badwords) { foreach ($replacement AS $replacements) { $str = eregi_replace($badwords, $replacements, $str); } } echo $str; For the sake of keeping this forum clean the words I have in my db are: filter_word: hello filter_replace: [was hello] filter_word: crap filter_replace: [was crap] but it is outputting: [was crap] i wish you would [was [was [was crap]]] off! Any suggestions? Quote Link to comment Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 Anyone? Quote Link to comment Share on other sites More sharing options...
kucing Posted February 24, 2007 Share Posted February 24, 2007 hello. Do you have any example how your bad words look like? K Quote Link to comment Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 ^^ They look like this in the db: filter_word: hello filter_replace: [was hello] filter_word: crap filter_replace: [was crap] filter_word is the bad word, and filter_replace is the word it should be replaced with. Quote Link to comment Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 Any suggestions? Please :'( lol Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 You're replacing the string with a larger string that contains it. You'd have to replace it with something that doesn't contain itself. So replace [crap] with [was c_r_a_p] or something. Quote Link to comment Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 Okay I changed the DB entries abit to: $str = "hello i wish you would crap off!"; filter_word: hello filter_replace: [removed] filter_word: crap filter_replace: [snipped] but I get this outputted: [snipped] i wish you would [snipped] off! Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted February 24, 2007 Share Posted February 24, 2007 seems to work pretty well... perhaps use ***** for everything instead of a badword change to specified for each word? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 Yeah is there a reason you need to specify what bad word it was? Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted February 24, 2007 Share Posted February 24, 2007 lol, kind of defeat the purpose eh? Man [was fook] you, go [was fook] yourself! Lol, would in theory still be showing the curse word, just adding was to it. Quote Link to comment Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 lol no I dont want to show that bad word, its just I want to put something else in there depending on what word it was. for instance, if it was crap, i might want to replace it with c**p. How can I get this to work. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 Try this: <?php while ($row = mysql_fetch_assoc($selectbadwords)){ $badwords[] = $row["filter_word"]; $replacements[] = $row["filter_replace"]; } foreach ($badwords AS $k=>$badword){ $str = str_replace($badword, $replacements[$k], $str); } echo $str; ?> I also changed the variable names to make more sense - an array is plural so it should be $badwords, not the other way round Quote Link to comment Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 Thanks that works perfectly Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.