phpregexcsvsplit

Split string on commas not inside double quotes


I want to split following string with a comma.

1,"x1",43,"tr","y,7"

Result array should be like following.

[
    0 => '1',
    1 => '"x1"',
    2 => '43',
    3 => '"tr"',
    4 => '"y,7"'
]

In short, it should not consider comma if it is between quotes.

If I use explode, I will get following result, which I don't want.

[
    0 => '1',
    1 => '"x1"',
    2 => '43',
    3 => '"tr"',
    4 => '"y',
    5 => '7"'
]

I am stuck here, please help.


Solution

  • Easy!! Your string is a CSV.

    Use $your_array=str_getcsv($your_string);