Jump to content

[SOLVED] Need help with preg_split and Regex


madThumbs

Recommended Posts

:confused: :confused: :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 
}

 

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
//)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.