Reputation: 4555
I have a csv file (really big) that I'm parsing with php.
Now is made like this.
x,y,z,value,etc
but sometimes there is this:
x,"blah,blah,blah",z,value,etc
doing this: explode(',',$string);
In case of a "" value also explode everything within.
array([0]=>x,[1]=>"blah,[2]=>blah,[3]=>blah"....)
What can I do to have this:
array([0]=>x,[1]=>"blah,blah,blah",[2]=>z....)
instead?
Thanks
Upvotes: 3
Views: 109
Reputation: 4704
If I were you and had to use the method you are saying. I would parse the string for text within the quotes via a regular expression.
Replace the , with a * (for example)
x,"blah*blah*blah",z,value,etc
then explode the string once again with the ,
Now you should get an approriate array but now you have the bla*bla*bla.
Then just do str_replace on the array
And in that way you should work..
This only applies if you have strict rules how to parse.. (in this case by explode);
Upvotes: 0
Reputation: 6771
Don't use explode, use fgetcsv.
For parsing just a string use str_getcsv if you have PHP >= 5.3.
Upvotes: 5