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 Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/ 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']; } Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193131 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? Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193137 Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 Anyone? Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193156 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 Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193160 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. Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193166 Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 Any suggestions? Please :'( lol Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193184 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. Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193190 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! Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193194 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? Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193197 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? Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193198 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. Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193199 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. Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193204 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 Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193207 Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 Thanks that works perfectly Link to comment https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/#findComment-193215 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.