Search code examples
phpcsvdouble-quotesfgetcsv

fail to read csv file with quotations using fgetcsv()


I have a csv file that contains double quotes. when I read it using php fgetcsv(), the fields contain quotation will retrun null:

while(($data=fgetcsv($handle,5000,'|'))!==false){
    echo $data[0];
}

and the csv file may look like this

25" H x 8" W |15" H x 5" W
17.5" H x 9" W | 5" H x 12" W

Could anybody give me some advices?


Solution

  • fgetcsv() defaults to using a " as an enclosure character, but you can change this to another different character (e.g. a tilde ~) that isn't used anywhere in your data; then the " won't be misinterpreted

    while(($data=fgetcsv($handle,5000,'|','~'))!==false){