Roee Posted July 15, 2010 Share Posted July 15, 2010 Hi, I have a row in my table (mysql) which contains something like this: {1,1,1},{2,2,2},{3,3,3},{4,4,4} How do I use reg exp to get an array of: $arr[0] = {1,1,1} $arr[1] = {2,2,2} $arr[2] = {3,3,3} $arr[3] = {4,4,4} ? And after getting this array, How do I split it to something like: $a[0][0] = 1 $a[0][1] = 1 $a[0][2] = 1 $a[1][0] = 2 $a[1][1] = 2 $a[1][2] = 2 $a[2][0] = 3 $a[2][1] = 3 $a[2][2] = 3 $a[3][0] = 4 $a[3][1] = 4 $a[3][2] = 4 Thank you very much Roee Link to comment https://forums.phpfreaks.com/topic/207873-a-question-in-regular-expression/ Share on other sites More sharing options...
wildteen88 Posted July 15, 2010 Share Posted July 15, 2010 As the bits you want are delimited by a comma you can use explode to separate them into an array. Link to comment https://forums.phpfreaks.com/topic/207873-a-question-in-regular-expression/#findComment-1086683 Share on other sites More sharing options...
Roee Posted July 15, 2010 Author Share Posted July 15, 2010 but not only the bits are delimited by a comma, they also contain commas in themselfs Link to comment https://forums.phpfreaks.com/topic/207873-a-question-in-regular-expression/#findComment-1086684 Share on other sites More sharing options...
wildteen88 Posted July 15, 2010 Share Posted July 15, 2010 $data = '{1,1,1},{2,2,2},{3,3,3},{4,4,4}'; $parts = explode('},{', $data); $arr = array(); foreach($parts as $part) { $part = str_replace(array('{','}'), '', $part); $bits = explode(',', $part); $arr[] = $bits; } echo '<pre>'.print_r($arr,true).'</pre>'; Link to comment https://forums.phpfreaks.com/topic/207873-a-question-in-regular-expression/#findComment-1086688 Share on other sites More sharing options...
Roee Posted July 15, 2010 Author Share Posted July 15, 2010 You really don't like regular expressions, do you ? I really think the reg ex is better for me here, beacuse all these loops waste so much time for my site which contains so many results. So I still look for someone who'll show me how to do it with Reg Ex. Thanks! Link to comment https://forums.phpfreaks.com/topic/207873-a-question-in-regular-expression/#findComment-1086697 Share on other sites More sharing options...
gwolgamott Posted July 15, 2010 Share Posted July 15, 2010 Try posting this in the REGEX sub forum. Link to comment https://forums.phpfreaks.com/topic/207873-a-question-in-regular-expression/#findComment-1086700 Share on other sites More sharing options...
Roee Posted July 16, 2010 Author Share Posted July 16, 2010 I tried something like this, and its work: preg_match_all('/{([1-9]),([1-9]),([1-9])}/', $string, $matches, PREG_SET_ORDER); but how do I change the [1-9] to something that will get 4-digit numbers and not only one digit numbers ? thank you Link to comment https://forums.phpfreaks.com/topic/207873-a-question-in-regular-expression/#findComment-1086973 Share on other sites More sharing options...
plutomed Posted July 16, 2010 Share Posted July 16, 2010 ignore Link to comment https://forums.phpfreaks.com/topic/207873-a-question-in-regular-expression/#findComment-1086980 Share on other sites More sharing options...
Roee Posted July 16, 2010 Author Share Posted July 16, 2010 nevermind, solved. Link to comment https://forums.phpfreaks.com/topic/207873-a-question-in-regular-expression/#findComment-1087005 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.