Welcome Guest, Not a member yet? Register   Sign In
csvreader library issue with importing to database
#1

[eluser]Unknown[/eluser]
I am using the csvreader library to import a .csv file into a database, but having issues with commas inside double quotes.

The csvreader is set to this:
var $fields; /** columns names retrieved after parsing */
var $separator = ';'; /** separator used to explode each line */
var $enclosure = '"';

With an example:
"col1";"col2";"col3"
"11";"12";"13"
"21;"22;"2;3"

My items in the row are not separated by a semi-colon, but replacing the separator with a comma severely screws things up. When I keep the semi-colon in place, I get issues because not every field in the line is encapsulated by double-quotes.


foreach($data['csvData'] as $row)
{
foreach ($row as $item)
{
$item=explode(",", $item);
$results_array = array(
'id' => $item[1],
'column_2' => $item[2],
etc etc.

A line from the csv file: 1, "value 1, value 2, value 3", something, something else

The explode function then messes up the database insert, causing 'value 2' to be inserted where 'something' should be.
I understand why this is happening, but I can't figure out how to remove the commas from inside the double quotes, or at least ignore them.




Theme © iAndrew 2016 - Forum software by © MyBB