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 
}

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.