Search code examples
phpencodingfgetcsv

Why does this CSV not be parsed with fgetcsv?


I cannot parse a CSV file which is well formated. Could it be that it has something to do with encoding? This is the Source:

<?php
$handle = fopen ("http://productdata.zanox.com/exportservice/v1/rest/20058589C1721570258.csv?ticket=A3AC91472561713FFB72A266542E9240AFE88CDE05D23B40B28B517606BE5D41&columnDelimiter=;&textQualifier=DoubleQuote&nullOutputFormat=NullValue&dateFormat=dd/MM/yyyy HH:mm:ss&decimalSeparator=comma&gZipCompress=null&id&na&pp&df&ds&im&lk&sn","r");  

while ( ($data = fgetcsv ($handle, 1000, ";")) !== FALSE ) {
  $num = count ($data);
  for ($c=0; $c < $num; $c++) {
    echo $data[$c].";";
  }
} 
?>

I guess, it has to do something with encoding. Output is : ‹{¿{?×(ÄN¾R"0;

This is the Running version: Problem with CSV


Solution

  • The newline character is the problem. You have unix chars, but you expect windows-style newlines. I converted an test it with the given CSV file.

    You can test the converted CSV file with windows newlines:
    http://pastebin.com/9CK3JMRc

    You could fix this in the by autodetection ini_set('auto_detect_line_endings', true); or convert the strings.