不带引号的 CSV 不适用于 fgetcsv
我正在尝试通过 PHP 解析用户上传的 CSV 文件,但它无法正常工作。
我已经上传了几个格式正确的 CSV,但效果很好;我有许多用户尝试导入从 Excel 导出的 CSV 文件,但他们遇到了问题。我将这些文件与我的文件进行了比较,发现 Excel 文件的条目周围都缺少引号。除此之外,它们是相同的。如果我打开它并使用 Open Office 保存它,而不进行任何更改,它就可以工作。所以我相当确定这与报价有关。
我的问题是;如何读取这些格式不正确的 CSV?
更新:原因已找到!
这是特定于 Mac 版本的 Excel 的。由于某些任意原因,Mac 上的换行符处理方式有所不同,因此在使用 fgetcsv 之前,您应该这样做;
ini_set('auto_detect_line_endings',TRUE);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是特定于 Mac 版本的 Excel 的。由于某些任意原因,Mac 上的换行符处理方式有所不同,因此在使用 fgetcsv 之前,您应该这样做;
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;
看看fgetcsv的手册页,它的原型是这样的:
默认值对于
$enclosure
(即第4个参数)是双引号。如果您尝试指定不需要任何封装,并为第四个参数指定空字符串,该怎么办?
(当然,这可能会破坏现在的工作方式——这意味着您必须处理两种不同的情况:字段用双引号引起来的文件,以及第一种情况无法读取的文件)
Looking at the manual page of
fgetcsv
, its prototype looks like this :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)