thedevilinu Posted December 5, 2010 Share Posted December 5, 2010 Hi I am trying to modify my database and am using the following part of code which isn't working. $bad is various values seperated by a newline. The strange thing about the code is that it works when their is only one $value but won't work if their are two or more values even though it is too easy to be wrong. Please help $badarray=explode(array("\r\n", "\r", "\n"),$bad); foreach($badarray as $value){ $value= str_replace(array("\r\n", "\r", "\n"),NULL,trim($value)); echo $value; $query="Update googlevacancies_junk SET exist='$no' WHERE email='$value' "; mysql_query($query); } Quote Link to comment Share on other sites More sharing options...
btherl Posted December 6, 2010 Share Posted December 6, 2010 Try this: $badarray=explode(array("\r\n", "\r", "\n"),$bad); foreach($badarray as $value){ $value= str_replace(array("\r\n", "\r", "\n"),NULL,trim($value)); echo $value; $query="Update googlevacancies_junk SET exist='$no' WHERE email='$value' "; mysql_query($query) or die("Error in query:<br>$query<br>" . mysql_error()); } The only change is to check for errors from mysql_query() Quote Link to comment Share on other sites More sharing options...
thedevilinu Posted December 7, 2010 Author Share Posted December 7, 2010 No error came when I put that. However, I noticed something funny. When $bad is insterted from textarea seperated by a newline in the form of 1@a.com 2@b.com .................. and then $badarray is formed by the above code, it doesn't work. It will only work if I have one value there. However if I manually define $badarray as $badarray=array('1@a.com','2@b.com'.......) IT WORKS Output of echo $view, however, remains same in both case Quote Link to comment Share on other sites More sharing options...
btherl Posted December 7, 2010 Share Posted December 7, 2010 That tells you there is a problem with your explode() then. Looking at the php manual, explode() does not take an array as the first argument. Quote Link to comment Share on other sites More sharing options...
thedevilinu Posted December 7, 2010 Author Share Posted December 7, 2010 Can you please explain "explode() does not take an array as the first argument"? This is the text that I found in php.net on explode function "Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter. " There is also an example pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = explode(" ", $pizza); echo $pieces[0]; // piece1 echo $pieces[1]; // piece2 The only difference in his pizza and mine is that it's pieces are seperated by a space and mine were seperated by a newline. Moreover, the funny thing is that echo $value works exacly same for $badarray in both cases. This is the first time I have faced a problem with explode. I use it very often to split things in to array including passing variables in the url for SEO purposes Quote Link to comment Share on other sites More sharing options...
btherl Posted December 7, 2010 Share Posted December 7, 2010 The problem is the part in bold: $badarray=explode(array("\r\n", "\r", "\n"),$bad); You can't put an array here - it has to be a string. Quote Link to comment Share on other sites More sharing options...
thedevilinu Posted December 14, 2010 Author Share Posted December 14, 2010 Ah!! You are right about that explode and array thing.. however here's a brilliant function i picked up which does exactly what I wanted to function multiexplode($delimiters,$string) { $return_array = Array($string); // The array to return $d_count = 0; while (isset($delimiters[$d_count])) // Loop to loop through all delimiters { $new_return_array = Array(); foreach($return_array as $el_to_split) // Explode all returned elements by the next delimiter { $put_in_new_return_array = explode($delimiters[$d_count],$el_to_split); foreach($put_in_new_return_array as $substr) // Put all the exploded elements in array to return { $new_return_array[] = $substr; } } $return_array = $new_return_array; // Replace the previous return array by the next version $d_count++; } return $return_array; // Return the exploded elements } 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.