explode csv, fields encapsulated with ' " '

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vcarter
Forum Newbie
Posts: 14
Joined: Sun Jun 21, 2009 3:02 pm

explode csv, fields encapsulated with ' " '

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: explode csv, fields encapsulated with ' " '

Post by requinix »

vcarter
Forum Newbie
Posts: 14
Joined: Sun Jun 21, 2009 3:02 pm

Re: explode csv, fields encapsulated with ' " '

Post 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 -
Post Reply