I am downloading and importing a remote text file to a local mysql database.
The format has recently changed to have a comma inside one of the columns and is causing the import to fail with error “Invalid field count in CSV input on line 1.” The data now looks like:
"bonita","FL","212025920","2012929","Hooker,Bill"
How do I update this code snippet to allow for a comma within quotes?
foreach(explode($lineseparator,$csvcontent) as $line) {
$lines++;
$line = trim($line," \t");
$line = str_replace("\r","",$line);
/************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************/
$line = str_replace("'","\'",$line);
/*************************************/
$linearray = explode($fieldseparator,$line);
$linemysql = implode("','",$linearray);
$linemysql = str_replace("\"","",$linemysql);###code added
$query = "insert ignore into $databasetable values('$linemysql');";
Rewrite your function to :
The php getcsv functions takes care of the delimiters and tracks the enclosures – saves you those headaches.
The following snippet:
outputs: