3

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

0plus1
  • 4,475
  • 12
  • 47
  • 89

2 Answers2

5

Don't use explode, use fgetcsv.

For parsing just a string use str_getcsv if you have PHP >= 5.3.

Marc
  • 6,749
  • 9
  • 47
  • 78
0

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);

BoqBoq
  • 4,564
  • 5
  • 23
  • 29