PHP CSV file separated by semicolon

I have a csv file with the following structure:

a; b; c,cc; d 

When I try to process it, it says that offset 2 and 3 are undefined. It took a little time to understand that this is caused , and has no idea how to solve it. If I delete,, everything will be fine.

Here is my processing function:

 function process_csv($file) { $file = fopen($file, "r"); $data = array(); while (!feof($file)) { $csvdata = fgetcsv($file); $data[] = explode(';', $csvdata[0]); } fclose($file); return $data; } 

Tried fgetcsv($file); like fgetcsv($file, '"'); but didn't help.

+9
php
source share
1 answer

Your problem is what fgetcsv uses as the default delimiter. If you change it to ; you do not need explode .

 function process_csv($file) { $file = fopen($file, "r"); $data = array(); while (!feof($file)) { $data[] = fgetcsv($file, null, ';'); } fclose($file); return $data; } 
+12
source share

All Articles