私はCSVファイルをMySQLデータベースにインポートしています。 CSVを解析するには、fgetcsv()
を使用しています。 CSVは、「エスケープされていないcharacetersと原因とを含んでいるerror array_combine(): Both parameters should have an equal number of elements
fgetcsvでエスケープされていない二重引用符を解析する
CSVデータはこの形式である:私は追加
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;
}
:
"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"
私のコード$escapestring='"'
しかし、それは助けになりませんでした。 ""文字がエスケープされていない "" TORINO ""からのエラーですか?もしそうなら、これらのフィールドを扱う方法はありますか?
それがあるかのように見えます""(二重二重引用符)は引用符として解析されます。意味、あなたはそれらを置き換える/除外することができ、あなたは行かなくてはなりません。 –
二重引用符がすでに二重引用符でエスケープされているので、 '$ escapestring'を渡す必要はありません。あなたはあなたが投稿した一行にfgetcsvを実行するだけでこれを見ることができ、正しい配列を得ることができます。ヘッダーを含むエラーにつながる最小のCSVを投稿できますか? –
csvファイルをさらに提供できますか?特にヘッダー。ヘッダーの重複したエントリ(空の文字列、数字)はものを混乱させる可能性があります。 – Deus777