Re: fgetcsv Help

From: Date: Wed, 11 Dec 2002 13:45:59 +0000
Subject: Re: fgetcsv Help
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-127800@lists.php.net to get a copy of this message
Richard, > I am parsing a csv file with fgetcsv and the fields are surrounding by > double quotes, now I am running into a problem periodically that when there > are quotes within the value it is treating it like another value instead of > the same value. > Any ideas on how to get around that? > > I am thinking I might have to convert all quotes to &quot; and then turn all > &quot;,&quot; back into quotes and then the first &quot; in the file and > then last one since obviously they will not have a comma in between them. > This seems like a while lot of work.. > Ideas? The first idea is that the system that outputs a .CSV with quotes within a quoted field is broken big-time and that's the part that needs fixing! I'm on the same wavelength as you describe: you can't use any automated csv function on such a file. Quotes <<should>> only appear in pairs. The closing quote must therefore appear immediately prior to a field separator (eg comma) - whitespace not withstanding, unless it is the last field on a line, in which case it will appear immediately prior to a record separator. One possibility is to write a 'filter' script to do this - and thus leave your existing code intact. The filter could take in the file, explode on quotes, and then cycle through the resultant array looking at the first non-whitespace data in every second element, clean/recode where necessary, and then implode and output. Alternatively could join/unset consecutive array elements which have been split inappropriately (by 'internal' quotes), and then implode using the other style of quotes/another separator, if this can be made to work later in the process... What a palaver! =dn

« previous php.general (#127800) next »