views:

351

answers:

2

I'm trying to parse CSV files uploaded by the user through PHP, but it's not working properly.

I've uploaded several properly formatted CSVs and it worked fine, however; I have many users trying to import CSV files exported from Excel and they are having problems. I've compared the files to mine and noticed that the Excel files all lack quotes around the entries. Aside from that, they are identical. If I open it and save it with Open Office, without making any changes at all it works. So I'm fairly certain it's related to the quotes.

My question is; how do I read these improperly formatted CSVs?

UPDATE: Cause has been found!

This is specific to the Mac version of Excel. Line breaks are handled differently on Macs for some arbitrary reason, so before using fgetcsv, you should do this;

ini_set('auto_detect_line_endings',TRUE);
+1  A: 

Looking at the manual page of fgetcsv, its prototype looks like this :

array fgetcsv  ( resource $handle  [, int $length  
    [, string $delimiter = ','  [, string $enclosure = '"' 
    [, string $escape = '\\'  ]]]] )

The default value for $enclosure (i.e. the 4th parameter) is a double-quote.

What if you try specifying that you don't want any enclosure, specifying an empty string for that 4th parameter ?

(Of course, this might break what's now working -- which means you'd have to deal with two separate cases : files with fields enclosed in double-quotes, and files that couldn't be read by the first case)

Pascal MARTIN
Yeah, I saw that. But, as you said, it breaks what currently works. Is there no simple way to check which format it's stored in and switch the behavior appropriately?
Stephen Belanger
I suppose some regex would allow you to determine which format a file is in ;; another solution would be to always try the first *(most frequent case)* solution, and, if it doesn't work, try the second one ;; considering all lines of your file are probably formated the same way, doing the test only for the first line should be enough -- which means you would only do 1 "useless" call to `fgetcsv`.
Pascal MARTIN
I tried that, it turns out blank enclosures don't work...
Stephen Belanger
+3  A: 

This is specific to the Mac version of Excel. Line breaks are handled differently on Macs for some arbitrary reason, so before using fgetcsv, you should do this;

ini_set('auto_detect_line_endings',TRUE);
Stephen Belanger