There are several ways to read CSV files with PHP. I used the break function to put each line in an array, and then explode the commas and use trim to remove any quotes around the data. It was dirty ...
PHP 5 now has fgetcsv and * str_getcsv * ... I assume this is the best way to do this these days, so I damaged the code ...
$fp = fopen($csv_file['file_path'], 'r');
while (($data = fgetcsv($fp, 0, "\r", '"', "\r")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
print_r(str_getcsv($data[$c]));
}
}
It seems to work, but is there a safer approach? For example, if it works, then line breaks are \ n or \ r ...
Any input you can give will be awesome!
source
share