tags:

views:

16

answers:

1

I'm working with a CSV file and PHP5 however I'm not quite understanding what I am seeing. The file is as follows when I open it in a text editor:

"Jun 23,2010 21:40","City1","Location1","0 0 0 "
"Jun 23,2010 21:41","City2","Location1","0 0 0 "

What I get as output from this code:

while (($data = fgetcsv($handle, 1000, ",","\"")) !== FALSE) {
        $num = count($data);
 echo "<p> $num fields in line $row: <br /></p>\n";
 $row++;
 for ($c=0; $c < $num; $c++) {
  echo $data[$c] . "<br />\n";
 }
}
fclose($handle);

Is this output:

 5 fields in line 1:
"Jun 23
2010 21:40"
City1
Location1
0 0 0 

 4 fields in line 2:
Jun 23,2010 21:41
City2
Location1
0 0 0 

As you can see, on the first line fgetcsv skips the enclosure character and reads the comma as the first field, whereas the 2nd line reads correctly and so do all the lines after that.

So am I missing something or is this a bug, and if it is a bug what would be some possible solutions other than rewriting the original file?

+1  A: 

Your code (full):

<?
$handle = fopen('test.csv', "r");
while (($data = fgetcsv($handle, 1000, ",","\"")) !== FALSE) {
    $num = count($data);
 echo "<p> $num fields in line $row: <br /></p>\n";
 $row++;
 for ($c=0; $c < $num; $c++) {
      echo $data[$c] . "<br />\n";
 }
}
fclose($handle);
?>

Parses the data you posted above correctly:

taifun:test killerx$ php test.php

4 fields in line :

Jun 23,2010 21:40
City1
Location1
0 0 0

4 fields in line 1:

Jun 23,2010 21:41
City2
Location1
0 0 0

Maybe you have a "misencoded" " sign. like “” or ‟ or ˝. They may look similar, but are not the same thing as ".

Killer_X
After checking around the code, I think you're right and something in the actual file might be screwing up the fgetcsv. I copy and pasted the whole file in a text editor to a new file, ran that and no problems. The file is being written by an iMacoros script and somehow it messes up the format on a binary level because in text they look identical.
ruskiar
Dug around a bit more and it turns out fgetcsv doesn't like UTF-8 encoding, so after a small file conversion everything works like a charm.
ruskiar