如何从PHP中的CSV文件中提取数据我有一个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" ...有什么优雅的方法吗?
3 回答
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
fgetcsv
$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);}
白衣染霜花
TA贡献1796条经验 获得超10个赞
$sfp = fopen('/path/to/source.csv','r');
$dfp = fopen('/path/to/destination.csv','w');
while ($row = fgetcsv($sfp,10000,",","")) {
$goodstuff = "";
$goodstuff = str_replace("¦",",",$row[2]);
$goodstuff .= "\n";
fwrite($dfp,$goodstuff);
}
fclose($sfp);
fclose($dfp);- 3 回答
- 0 关注
- 784 浏览
添加回答
举报
0/150
提交
取消
