我有一个看起来像这样的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" ... 吗?有什么优雅的方法吗?
split($lines[0],",")
"text" ,"with commas" ...
您可以fgetcsv用来解析CSV文件,而不必担心自己解析。
fgetcsv
PHP手册中的示例:
$row = 1; if (($handle = fopen("test.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br />\n"; } } fclose($handle); }