如何用逗号分隔 CSV 行的值?
"AMZN","Amazon.com, Inc.",211.22,"11/9/2011","4:00pm","-6.77 - -3.11%",4673052
Amazon.com, Inc. 被视为 2 个值而不是一个。
我尝试了这个 $data =explode( ',', $s);
如何修改它以避免值问题中的逗号?
"AMZN","Amazon.com, Inc.",211.22,"11/9/2011","4:00pm","-6.77 - -3.11%",4673052
Amazon.com, Inc. is being treated as 2 values instead of one.
I tried this $data = explode( ',', $s);
How do I modify this to avoid the comma in the value issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能应该查看 str_getcsv() (或 fgetcsv() 如果您从文件加载 CSV)
这会将 CSV 内容读取到数组中,而不需要爆炸等。
编辑-扩展佩卡提出的观点,如果您正在使用 PHP < 5.3
str_getcsv()
不起作用,但有一个有趣的方法 这里重现了较低版本的功能。另一种方法此处使用fgetcsv()
创建临时文件后。You should probably look into str_getcsv() (or fgetcsv() if you're loading the CSV from a file)
This will read the CSV contents into an array without the need for exploding etc.
Edit- to expand upon the point made by Pekka, if you're using PHP < 5.3
str_getcsv()
won't work but there's an interesting approach here which reproduces the functionality for lesser versions. And another approach here which usesfgetcsv()
after creating a temporary file.使用专用的 CSV 库。人们一遍又一遍地解释说,手动解析 CSV 等文件格式会带来麻烦,因为您不知道 CSV 的所有变体以及正确执行此操作的所有规则。
Use a dedicated CSV library. It's been explained over and over that parsing file formats like CSV manually is asking for trouble, because you don't know all the variations of CSV and all the rules to do it right.