如何用EXPLODE CSV行用逗号的值? [英] How do you EXPLODE CSV line with a comma in value?
问题描述
"AMZN","Amazon.com, Inc.",211.22,"11/9/2011","4:00pm","-6.77 - -3.11%",4673052
Amazon.com,Inc。被视为2个值的一个。
Amazon.com, Inc. is being treated as 2 values instead of one.
我试过这个 $ data = explode(',',$ s);
如何修改此值以避免值的问题中的逗号?
How do I modify this to avoid the comma in the value issue?
推荐答案
可能会查看 str_getcsv()(或
You should probably look into str_getcsv() (or fgetcsv() if you're loading the CSV from a file)
这将读取一个文件(如果你正在从一个文件加载CSV文件的话).net / manual / en / function.fgetcsv.php> fgetcsv将CSV内容转换为数组而不需要展开等。
This will read the CSV contents into an array without the need for exploding etc.
编辑 - 展开Pekka的观点,如果你使用PHP& 5.3 str_getcsv()
将不起作用,但有一个有趣的方法这里再现较小版本的功能。另一种这里使用 fgetcsv()
创建一个临时文件。
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.
这篇关于如何用EXPLODE CSV行用逗号的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!