简体   繁体   English

在PHP上处理CSV文件的正确方法

[英]Correct way to handle CSV Files on PHP

Hi I have the following brain braking thing going on. 嗨,我正在进行以下令人震惊的事情。 The thig is that I'm developing a Laravel Application that imports and exports CSV files. 想法是,我正在开发可导入和导出CSV文件的Laravel应用程序。 Now, the data that the application Imports/Exports(I/E now on) has fields from various data types, we have text and numbers, now the text can contain commas(,) and using the default CSV separator (,) on php can lead to fields on the import to generate incorrectly. 现在,应用程序Imports / Exports(现在是I / E)的数据具有来自各种数据类型的字段,我们有文本和数字,现在文本可以包含逗号(,)并在php上使用默认的CSV分隔符(,)可能导致导入中的字段生成不正确。 The client suggested that II/E using ^ as a separator for the export and (,) again for the import of the data. 客户建议II / E使用^作为导出的分隔符​​,并再次使用(,)导入数据。 Now, my question is, can I trust when I/E data using the default separator? 现在,我的问题是,我可以信任使用默认分隔符的I / E数据吗? Can anyone suggest a best way to do the I/E process? 任何人都可以建议执行I / E流程的最佳方法吗?

Edit The client main struggle is because he uses Excel on a Mac to edit the CSV files, now on my Mac, I can easily edit the files without any issues regarding the separator, of course if the separator is a comma (,) but if we use the ^ as a separator then my excel is a mess and he's ommit some fields. 编辑客户端的主要难题是因为他在Mac上使用Excel编辑CSV文件,现在在我的Mac上,我可以轻松编辑文件而没有任何与分隔符有关的问题,当然,如果分隔符是逗号(,),但是如果我们使用^作为分隔符,那么我的excel就是一团糟,他省略了一些字段。

Thanks in advance. 提前致谢。

Don't re-invent the wheel. 不要重新发明轮子。 Re-use a well-written well-tested package. 重新使用编写良好且经过测试的程序包。 On good one is CSV from The PHP League . 最好的是来自PHP League的 CSV

(Historical note about delimiters: the most overlooked (for 50+ years) feature in computing is that the ASCII charset (and therefore UTF8 too) assigned specific chars for delimiting fields (or units, as they called them) and records ... and even groups of records and entire files. See https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text . But instead folks didnt RTM and so used commas, etc to separate fields and newlines (\\r, \\n, \\r\\n) to separate records. D-oh!!! So, if you are able to select your own delimiters and want to be safe by using a char not used for any other purpose, use the ASCII delimiters.) (有关定界符的历史记录:50年来最被忽视的功能是ASCII字符集(因此也称为UTF8)为定界字段(或单位,如他们所称的)和记录分配了特定的字符...和甚至是记录和整个文件的组。请参阅https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text ,但是人们并没有使用RTM,因此使用逗号等分隔字段和换行符(\\ r,\\ n,\\ r \\ n)分隔记录D-哦!!!因此,如果您能够选择自己的定界符,并且希望通过使用不用于其他任何目的的char来确保安全,请使用ASCII定界符。)

There is no such thing as a "CSV standard". 没有“ CSV标准”之类的东西。 Therefore, having a "default" comma is not exactly true. 因此,使用“默认”逗号并不完全正确。 One can basically use whatever one likes, and the column and line seperaters as well as the enclosures for values or complete lines really depend on what you are planning to put in as data. 基本上可以使用任何喜欢的东西,列和行分隔符以及值或完整行的外壳实际上取决于您打算作为数据放入的内容。

TL;DR: It is totally up to you and your client, what you are using as those characters. TL; DR:这完全取决于您和您的客户,您使用的是那些字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM