|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-06-22 00:44 UTC] iliaa@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 06:00:01 2025 UTC |
Description: ------------ Assume a file with 1 line of text (toImport.csv) and a [newline] after the last char in that line. The code below will echo "2" (Lines) using either fgets or fgetcsv. While this is right for fgets, it's wrong for fgetcsv (I think), because in CSV-Files the [newline] (logically) terminates a row, it doesn't start a new one - at least if you think of a CSV-File as a Recordset from a SQL-Statement (If you read the last "line" of a SQL-Result, move the Row-Pointer to the next row and try to read it, you'll get false/EOF and not a row with 0 elements). If you return the Array "$data" in each iteration you get a array with (int) 0 elements as last row, what makes using a loop until EOF impossible, because you (additionally) have to check the number of elements returned in $data. It's quite anoying, that fgetcsv makes difference if a csv-file is terminated by a newline or not. Reproduce code: --------------- $fh = fopen(dirname(__FILE__)."/toImport.csv","r"); $row = 0; while (!feof($fh)) { $data = fgetcsv($fh,1000,";"); // $data = fgets($fh,1000); $row++; } echo($row); Expected result: ---------------- fgetcsv should echo 1, while fgets should echo 2