我有一个csv文件,看起来像这样
$lines[0] = "text,with commas","another text",123,"text",5; $lines[1] = "some without commas","text"; $lines[2] = "some text with commas or no","text";
我想要一张桌子:
$t[0] = array("text,"123","5"); $t[1] = array("some without commas","text"); $t[2] = array("some text,with comma,s or no",NULL,"text");
如果我使用split($lines [0],“,”)我会得到“文本”,“用逗号”…
有什么优雅的方式吗?
您可以使用
fgetcsv
来解析CSV文件,而无需担心自己解析.
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); }