【发布时间】:2011-02-17 19:48:06
【问题描述】:
我有一个 csv 文件,看起来像这样
$lines[0] = "text, with commas", "another text", 123, "text",5;
$lines[1] = "some without commas", "another text", 123, "text";
$lines[2] = "some text with commas or no",, 123, "text";
我想要一张桌子:
$t[0] = array("text, with commas", "another text", "123", "text","5");
$t[1] = array("some without commas", "another text", "123", "text");
$t[2] = array("some text, with comma,s or no", NULL , "123", "text");
如果我使用split($lines[0],","),我会得到"text" ,"with commas" ...
有什么优雅的方法吗?
【问题讨论】:
-
使用
explode而不是split -
使用为此目的构建的工具(例如
fgetcsv来命名)而不是explode -
一种方法(虽然 fgetcsv 更好)是加入相邻的字段,直到合并的字段以引号结尾,如果字段以引号开头...