How to practically split values ​​from a CSV file into a MySQL database

Suppose I have the following line in a CSV file (I deleted the header line in this example):

"500,000",2,50,2,90000

I have a PHP script to read a CSV file, split the file into separate lines and save each line in an array called $ linearray. Then I use the foreach loop for each individual line.

Inside the foreach loop, I am breaking the string into separate variables using the following function:

$line = str_replace("'","\'",$line);

Here I insert values ​​into separate columns in a MySQL database. The script is running. Values ​​are inserted into the database, but I am facing a problem. I want to:

"500,000"  |  2  |  50  |  2  |  90000

But I get this:

"500  |  000"  |  2  |  50  |  2  |  90000

The script is not smart enough to realize that it should be missing quotation marks around quotes. Do you know how I can change my script to make sure that I get the output I'm looking for?

+3
2

str_getcsv

print_r(str_getcsv($line));

:

Array
(
    [0] => 500,000
    [1] => 2
    [2] => 50
    [3] => 2
    [4] => 90000
)

fgetcsv(), , fgetcsv(), .

+2
+1

Source: https://habr.com/ru/post/1750425/


All Articles