I'm writting a PHP script to import a CSV file to MySQL table.
My problem is that fields are separated by commas, but some fields came within an extra comma, or double quotes (I alread found the solution for double quotes), But not for extra commas. This is an example:
SORT_COLUMN,SORT_ALPHA,GUEST_NAME,PROFILE_ID,FULL_ADDRESS,IS_TOTAL_YN
0,DIAZ REBOLLO ANGELICA,Diaz Rebollo Angelica,824990,"Blvd. Manuel Avila Camacho 36, piso 10 Lomas de Chapul DF 11000"
As you can see in the FULL ADDRESS field:
"Blvd. Manuel Avila Camacho 36, piso 10 Lomas de Chapul DF 11000"
Comes with "
and a comma before 36,
This is part of the PHP CODE:
$lineseparator = "
";
foreach(explode($lineseparator,$csvcontent) as $line) {
$lines++;
$line = trim($line," \t");
$line = str_replace("","",$line);
// Here I remove double quotes in the csv file
$line = str_replace(""",'',$line);
$linearray = explode($fieldseparator,$line);
$linemysql = implode("','",$linearray);
$query = "insert into $databasetable values('$linemysql');";
$queries .= $query . "
";
@mysql_query($query);
}