aTeam Posted February 18, 2009 Share Posted February 18, 2009 Hi All, I am trying to get a string from a bad word filter into an array so i can sort the data then email it. i.e. after a Bad word filter it leaves my data full of gaps so i believe if i can sort the data in alphabetical order it will remove all these gaps. everything works except the sort function and i don't understand why, i think i am pretty close what do you guys think? $sql = 'SELECT * FROM `jos_facileforms_subrecords`' . ' WHERE element = 158 LIMIT 0, 30 '; // execute SQL query and get result $sql_result = mysql_query($sql,$connection) or die(mysql_error()); // Loop through the data set and extract each row in to it's own variable set $names = ""; while ($row = mysql_fetch_array($sql_result)) { $names .= $row['value'] . "\n" ; } function filter($string) { $badwords=array("ass","babi"); for ($i=0;$i < count($badwords);$i++){ $string = str_replace($badwords[$i],str_repeat("",strlen($badwords[$i])),$string); } return $string; } $filtered_output = filter($names); $names_array= explode(" ", $filtered_output); sort($names_array); // Email results $to = "[email protected]"; $subject = "XXXX"; $body = "$names_array"; -Adam Link to comment https://forums.phpfreaks.com/topic/145741-putting-a-string-into-an-array/ Share on other sites More sharing options...
Brian W Posted February 18, 2009 Share Posted February 18, 2009 add in the filter loop something like $string = str_replace(" ", " ", $string); that'll replace double spaces with a single space. btw, your filter will currently make "assassin" into "in" Both problems may be solved by changing your array to be $badwords=array(" ass "," babi "); or you regular expressions. Link to comment https://forums.phpfreaks.com/topic/145741-putting-a-string-into-an-array/#findComment-765233 Share on other sites More sharing options...
shadiadiph Posted February 18, 2009 Share Posted February 18, 2009 have you looked at this http://th.php.net/manual/en/function.asort.php looks like asort() sorts them alphabetically but don't hold me to that i have never used it b4 I never knew babi was a bad word Link to comment https://forums.phpfreaks.com/topic/145741-putting-a-string-into-an-array/#findComment-765234 Share on other sites More sharing options...
Brian W Posted February 18, 2009 Share Posted February 18, 2009 You should all be ashamed of yourselves for saying that word on a decent forum! Best resource for naughty word filters Link to comment https://forums.phpfreaks.com/topic/145741-putting-a-string-into-an-array/#findComment-765240 Share on other sites More sharing options...
aTeam Posted February 21, 2009 Author Share Posted February 21, 2009 Hi, lol yeah i have been through so much documentation it hurts, but i have picked up a few things and i have changed my tac. Instead of filtering after the data has been extracted i think it would be better to directly replace "bad words" in the database that way you can just use the "ORDER BY" function, instead of trying to sort a string. But that brings me to my next problem, i cant seem to pass the array into the mySQL query properly. The query works when i hard code it (i.e. write what i want to to find directly in the code) but it wont accept the value from the array of bad words. I am sure this is a syntax thing to do with the LIKE function but i have been looking for ages and i keep failing. what u guys think? // array of bad words $badwords=array("ass","babbi"); foreach ($badwords as $b) { //Fileter SQL $filter_sql = 'UPDATE jos_facileforms_subrecords SET value = \'XXX\'' . ' WHERE value LIKE \'"%.$b.%"\''; // execute $filter_sql query and get result $filter_sql_result = mysql_query($filter_sql,$connection) or die(mysql_error()); } -Adam Link to comment https://forums.phpfreaks.com/topic/145741-putting-a-string-into-an-array/#findComment-767680 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.