Page 1 of 1

explode csv, fields encapsulated with ' " '

Posted: Tue Apr 27, 2010 7:09 pm
by vcarter
Hi all -

I have a script that I've written, that takes a CSV file, does some stuff, and then spits it back out as a tab delimited text file. After a few days of the file going wonky on me, I realized that one of the fields (at least one, maybe more, but I think just one) could contain commas. Ugh. So now, I need to figure out how to change this stuff around, so that it it looks for encapsulated fields and does the same stuff as before.

Here is just a snippet of the code -- I can handle implementing whatever the fix is, into the rest of the code. This particular snippet existed so that it would replace values of "+" with the appropriate invoice number. Then I go back later and run through the file again, having set this initial invoice number.

Nonetheless, here's the code:

Code: Select all

$customerData=fgets($handleReadCA);
$temp=explode(",",$customerData);
$invoiceNum=$temp[0];
$value=implode("\t",$temp); 
fputs($handleWriteCA, $value);
I have no idea how to make this work when dealing with double quoted fields that contain commas. Any help/assistance would be greatly appreciated.

Re: explode csv, fields encapsulated with ' " '

Posted: Tue Apr 27, 2010 7:10 pm
by requinix

Re: explode csv, fields encapsulated with ' " '

Posted: Tue Apr 27, 2010 7:48 pm
by vcarter
tasairis wrote:fgetcsv
Right. I apparently don't understand how to implement that, which was why I tried here. :) Could you help me with adding/editing the code I provided above? I tried it, and got errors (array to string errors)

Thanks -