madThumbs Posted July 17, 2009 Share Posted July 17, 2009 :confused: Hey guys, really need your help here. I am try to split the following string into an array by comma, however, as you can see the sample below, in the [1] there are already a comma inside the sub string, which I don't want to split apart, so how can I layout this in regx, please help me out here. Thank you very much. $row_data = '16603, "Lay's, Inc", 2505 Anthem Village Dr. E-525, 12/15/2008 ' ; Ideal result: Array { [0] =>16603 [1] =>"Lay's, Inc" [2] =>2505 Anthem Village Dr. E-525 [3] =>12/15/2008 } what I got by split only by comma Array { [0] => 16603 [1] => "Lay's [2] => Inc" [3] => 2505 Anthem Village Dr. E-525 [4] =>12/15/2008 } Link to comment https://forums.phpfreaks.com/topic/166283-solved-need-help-with-preg_split-and-regex/ Share on other sites More sharing options...
nrg_alpha Posted July 17, 2009 Share Posted July 17, 2009 You can use preg_split to accomplish this: $row_data = '16603, "Lay\'s, Inc", 2505 Anthem Village Dr. E-525, 12/15/2008 ' ; $arr = preg_split('#,\s{2,}#', $row_data); $arr = array_map('trim', $arr); echo '<pre>'.print_r($arr, true); //Output: //Array //( // [0] => 16603 // [1] => "Lay's, Inc" // [2] => 2505 Anthem Village Dr. E-525 // [3] => 12/15/2008 //) Link to comment https://forums.phpfreaks.com/topic/166283-solved-need-help-with-preg_split-and-regex/#findComment-877091 Share on other sites More sharing options...
madThumbs Posted July 17, 2009 Author Share Posted July 17, 2009 Thanks you very much :D Link to comment https://forums.phpfreaks.com/topic/166283-solved-need-help-with-preg_split-and-regex/#findComment-877105 Share on other sites More sharing options...
nrg_alpha Posted July 17, 2009 Share Posted July 17, 2009 Your welcome Please flag this thread as 'Topic Solved'. Link to comment https://forums.phpfreaks.com/topic/166283-solved-need-help-with-preg_split-and-regex/#findComment-877106 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.