我有以下csv文件:
"Id","Title","Body","Tags"
"101","this title","
\"">.</>"";
","c# asp.net excel table"我想把它转换成一个数组,如下所示:
Array
(
[0] => Array
(
[0] => Id
[1] => Title
[2] => Body
[3] => Tags
)
[1] => Array
(
[0] => 101
[1] => this title
[2] => \"">.</>"";
[3] => c# asp.net excel table
)
)我的代码是:
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$data[$c] = strip_tags($data[$c]);
}
$result[$row] = $data;
$row++;
}
fclose($handle);
return $result;我的问题是我得到了以下数组:
Array
(
[0] => Array
(
[0] => Id
[1] => Title
[2] => Body
[3] => Tags
)
[1] => Array
(
[0] => 101
[1] => this title
[2] =>
\">.</>"";
)
[2] => Array
(
[0] => ,c# asp.net excel table"
)
)通常,当字段中可能存在代码时(这是一个StackOverflow数据转储,因此一些文本字段包含各种编程代码),如何避免检测到过多的recors。
发布于 2013-10-01 22:14:25
此字符串未正确转义:
"
\"">.</>"";
"发布于 2013-10-01 23:33:49
尝试使用CSVed打开该文件,以确保其格式正确。
如果CSV损坏了,那么您可以对解析的结果进行一些快速修复。例如:
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$data[$c] = strip_tags($data[$c]);
}
if (count($data) == 3) {
$data[1][2] .= $data[2].[0];
unset($data[2]);
}
$result[$row] = $data;
$row++;
}
fclose($handle);
return $result;https://stackoverflow.com/questions/19117362
复制相似问题