I have a CSV file I'm importing but am running into an issue. The data is in the format:

TEST 690, "This is a test 1, 2 and 3" ,$14.95 ,4

I need to be able to explode by the , that are not within the quotes...

Comments

One thing I would try is, if you can, change the input file so that EVERYTHING is in quotes, and then you can explode by "," after pulling off the first and last quotes. That way, it won't explode the commas not immediately next to quotes. Of course, that's only if you don't want to use fgetcsv like Artefacto suggests and you want to challenge yourself with it.

Written by Jeff Rupert

I can't make everything wrapped in quotes, it's exported that way through another system.

Written by Webnet

Are quotes only possible on the second field?

Written by Atømix

According to commonly accepted CSV spec, quotes are optional, and only required to disambiguate a field that has quotes, commas or multiple lines in it. en.wikipedia.org/wiki/Comma-separated_values#Basic_rules

Written by beporter

Accepted Answer

See the fgetcsv function.

If you already have a string, you can create a stream that wraps it and then use fgetcsv. See http://code.google.com/p/phpstringstream/source/browse/trunk/stringstream.php

Written by Artefacto
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki