I built an app that takes in a CSV as a bulk feed and I’ve notice a difference in CSVs when it is saved as a “Windows Comma Separated” vs. “Comma Separated” when saving in Excel on Mac. My script can only parse the Windows version and I can’t see how to overcome this in my code.
I found out that they’re different in the lines returns: http://cl.ly/image/3x463O3b0A1s
Here is the filetype list in Mac Excel: http://cl.ly/image/2F110Q1X0V0i
I explode it into lines like so:
$lines = explode("\n", $content);
With the regular CSV, it doesn’t recognize any lines, but with the Windows version it works as expected (go figure!). How do I fix this?
As mentioned before, you should use
fgetcsv()… However, I still had problems with CSVs generated on Macs using that function.PHP was not properly detecting line endings. To overcome the issue, I had to add
ini_set("auto_detect_line_endings", true);to the function that was processing the CSV file.