我正在将CSV文件导入MySQL数据库。分析我使用的CSV。CSV包含“未被转义的字符并导致和fgetcsv()
CSV数据的格式如下:

"GR109     "," ",0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0," ","GRANT     ","M          ","W",0,0.0,0.0,0.0,0.0,0.0,0,0,0,0,0,0.0,0.0," ",0,0,0.0," "," "," ",2.42,0.0,0.0,0.0,0.0," "," "," "," "," "," ",0.0,0.0,0.0,0.0,0.0," "," "," "," ","SELF COL  ","16 P PR.  "," ","PLAIN     "," ","R/E1ROW   "," "," "," "," "," "," "," ","R/E1ROW   ","BEADED    "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","GRANT     ","GRANT     "," "," ","  "," ",0.0,"  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","VAMP LNG - BLK. CARAVELLE P/S. QTR. LNG./ TNG.LINING - BLK. TORINO. (GREY ""TORINO"" FOR LIZARD.)","GR109 COMPLETE"

我的代码:
function csv_to_array($filename='', $delimiter=',', $enclosure='"', $escapestring='"')
{
if(!file_exists($filename) || !is_readable($filename))
    return FALSE;

$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
    while (($row = fgetcsv($handle, 1000, $delimiter, $enclosure, $escapestring)) !== FALSE)
    {
        if(!$header)
            $header = $row;
        else
            $data[] = array_combine($header, $row);
    }
    fclose($handle);
}
return $data;

}
我添加了error array_combine(): Both parameters should have an equal number of elements,但没有帮助。是不是“TORINO”中的错误,其中“字符”没有转义?如果是,有办法处理这些领域吗?

最佳答案

好 啊。我想我找到了。肯定是fgetcsv中的第二个参数,即行的长度,有点混乱。只需将其更改为0(函数可能工作得很慢),或者将其加倍。
1000年时,它将每一行分成两行,一行的长度正好是1000个字符(即使在世界的中部也会进行剪切),第二行是换行字符的其余部分。所以$row变量最初是一个长度为117的数组,然后是大约13,同样是117和13。
只要改变这个:

while (($row = fgetcsv($handle, 1000, $delimiter, $enclosure, $escapestring)) !== FALSE)

对此:
while (($row = fgetcsv($handle, 0, $delimiter, $enclosure, $escapestring)) !== FALSE)

关于php - 使用fgetcsv解析未转义的双引号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37132877/

10-11 20:06