How to avoid comma for CSV, fgetcsv () issue

I am using the fgetcsv () function in PHP to read a CSV file that is created using Microsoft Excel.

The problem is that the CSV file contains a comma value and it reads the escaped value (with double quotes) as it is.

For example: "2,5,7" reads as "2 and 5 and 8"

The problem can be resolved by changing double quotes to single quotation mark. However, I do not want to edit every CSV file every time.

The code I use is as follows:

  $ handle = fopen ($ path, "r");
 $ data = fgetcsv ($ handle, 1000, ",", "'");
 do
 {
     ** READ CSV **
 }
 while ($ data = fgetcsv ($ handle, 5000, ","));

What would be the best way to deal with this problem?

+4
source share
1 answer

Change this line

 $data = fgetcsv($handle,1000,",","'"); 

to

 $data = fgetcsv($handle,1000,",",'"'); 

or just leave the last two options as they are the default values

 $data = fgetcsv($handle,1000); 

The fourth parameter for fgetcsv is a shell character that indicates how to create fields that can have a separator inside them.

+7
source

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


All Articles