How to Practically Split Values from CSV File into MySQL Database
Posted
by Ryan
on Stack Overflow
See other posts from Stack Overflow
or by Ryan
Published on 2010-06-17T04:33:48Z
Indexed on
2010/06/17
4:43 UTC
Read the original article
Hit count: 268
Let's suppose I have the following line in a CSV file (I removed the header row in this example):
"500,000",2,50,2,90000
I have a PHP script read the CSV file, break the file into individual lines, and store each line in an array called $linearray. Then, I use a foreach loop to look at each line individually.
Within the foreach loop, I break the line into separate variables using the following function:
$line = str_replace("'","\'",$line);
From here, I insert the values into separate columns within a MySQL database. The script works. The values are inserted into a database, but I run into a problem. I want:
"500,000" | 2 | 50 | 2 | 90000
But I get this:
"500 | 000" | 2 | 50 | 2 | 90000
The script isn't smart enough to understand it should skip commas within quotation marks. Do you know how I can alter my script to make sure I get the output I'm looking for?
Thanks.
© Stack Overflow or respective owner