fgetcsv fails to read line ending in mac formatted csv file, any better solution?

PhpFgetcsv

Php Problem Overview


I was parsing a csv file using php with fgetcsv function. It parsed all content in a line, later i found, csv contains carraige return as "\r". I saw - it was reported as php bug before. I've solved this by setting php runtime configuration which is -

ini_set("auto_detect_line_endings", "1");

is there any more solution or is this the right way?

Thanks

Php Solutions


Solution 1 - Php

Setting auto_detect_line_endings is explicitly recommended by the http://php.net/manual/en/function.fgetcsv.php">php documentation.

However, I cannot fathom why you would want to delimit lines with \r in 2010. If possible, convert them to the UNIX-style \n.

Solution 2 - Php

\r line endings are created by Microsoft Excel when saving as a CSV file, so there is not much getting around that if you are starting with an Excel spreadsheet.

Using auto_detect_line_endings works fine, or you can normalize line endings with preg_replace("/\r\n|\n\r|\n|\r/", "\n", $subject);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMusaView Question on Stackoverflow
Solution 1 - PhpphihagView Answer on Stackoverflow
Solution 2 - PhpspekaryView Answer on Stackoverflow