假设我有一个.csv包含以下内容的文件: "text, with commas","another text",123,"text",5; "some without commas","another text",123,"text"; "some text with commas or no",,123,"text"; 如何通过PHP解析内容?
3 回答

千万里不及你
TA贡献1784条经验 获得超9个赞
由于PHP> = 5.3.0,答案要短一些:
$csvFile = file('../somefile.csv');
$data = [];
foreach ($csvFile as $line) {
$data[] = str_getcsv($line);
}

一只萌萌小番薯
TA贡献1795条经验 获得超7个赞
刚刚发现了一种在解析时获取索引的便捷方法。我的主意被炸了。
$handle = fopen("test.csv", "r");
for ($i = 0; $row = fgetcsv($handle ); ++$i) {
// Do something will $row array
}
fclose($handle);
- 3 回答
- 0 关注
- 612 浏览
添加回答
举报
0/150
提交
取消