考虑像这样的CSV文件
item1,"description 1"
item2,"description 2"
item3,"description 3
description 3 continues on new line"
item4,"description 4"
Run Code Online (Sandbox Code Playgroud)
哪个应该像这样解析
item1,"description 1"
item2,"description 2"
item3,"description 3 description 3 continues on new line"
item4,"description 4"
Run Code Online (Sandbox Code Playgroud)
有没有办法在PHP中解析具有多行值的CSV?
Sta*_*eyD 11
以下是一些如何做的工作示例.我正在使用fgetcsv:
字符串变量$ CsvString中的CSV:
$fp = tmpfile();
fwrite($fp, $CsvString);
rewind($fp); //rewind to process CSV
while (($row = fgetcsv($fp, 0)) !== FALSE) {
print_r($row);
}
Run Code Online (Sandbox Code Playgroud)
文件中的CSV:
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($row = fgetcsv($handle, 0, ",")) !== FALSE) {
print_r($row);
}
fclose($handle);
}
Run Code Online (Sandbox Code Playgroud)
基于StanleyD的答案,但使用临时内存块(而不是写入磁盘)以获得更好的性能:
$fp = fopen('php://temp','r+');
fwrite($fp, $CsvString);
rewind($fp); //rewind to process CSV
while (($row = fgetcsv($fp, 0)) !== FALSE) {
print_r($row);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10892 次 |
| 最近记录: |