Quy
Quy

Reputation: 29

Explode a comma-delimited string into an array without breaking up double quoted substrings

Suppose that I have this string:

var1, var2, var3, "var4, test, test2", var5, "var6", var7

How can I use explode() function to put them into an array like this

Array
(
    [0] => var1
    [1] => var2
    [2] => var3
    [3] => var4, test, test2
    [4] => var4
    [5] => var6
    [6] => var7
)

Upvotes: 0

Views: 781

Answers (2)

Emil Vikström
Emil Vikström

Reputation: 91983

Use the str_getcsv function:

$array = str_getcsv('var1, var2, var3, "var4, test, test2", var5, "var6", var7');

Upvotes: 1

Konrad Neuwirth
Konrad Neuwirth

Reputation: 898

I think str_getcsv() is the function you are looking for; it should do exactly what you want.

Upvotes: 5

Related Questions