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

六眼飞鱼酱① 提交于 2020-07-04 06:08:46

问题


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


回答1:


Setting auto_detect_line_endings is explicitly recommended by the 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.




回答2:


\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);



来源:https://stackoverflow.com/questions/4541749/fgetcsv-fails-to-read-line-ending-in-mac-formatted-csv-file-any-better-solution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!