我有以下csv文件:PHP fgetcsv - 檢測記錄太多
"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。
所以解決方法是搜索所有引號字符,並添加反斜槓,除非它匹配我的正確的行分隔符(「,」)? – tucson