如何用逗号分隔 CSV 行的值?

发布于 2024-12-15 02:04:16 字数 239 浏览 2 评论 0原文

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情废物 2024-12-22 02:04:16

您可能应该查看 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 uses fgetcsv() after creating a temporary file.

眼泪淡了忧伤 2024-12-22 02:04:16

使用专用的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文