For Programmers: Free Programming Magazines  


Home > Archive > PHP Documentation > March 2004 > #27730 [Opn->Csd]: fgetcsv() fails on 2 newlines









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author #27730 [Opn->Csd]: fgetcsv() fails on 2 newlines
rasmus@php.net

2004-03-28, 10:14 pm

ID: 27730
Updated by: rasmus@php.net
Reported By: clemens at gutweiler dot net
-Status: Open
+Status: Closed
Bug Type: Documentation problem
Operating System: Linux 2.6.3 (and 2.4.20)
PHP Version: 4.3.5
New Comment:

Fixed in the docs in CVS


Previous Comments:
------------------------------------------------------------------------

[2004-03-28 12:03:39] rasmus@php.net

This isn't actually a bug. I have changed this to a documentation
problem as the example on the fgetcsv() page is not very good. The
text description of the function is however correct. You get a
zero-element array for blank lines and FALSE on end of file. So your
code should have a loop like this:

while( ($d = fgetcsv( $fp, 4096, ';' )) !== FALSE )

------------------------------------------------------------------------

[2004-03-27 08:50:09] clemens at gutweiler dot net

Description:
------------
fgetcsv() breaks reading when a line has no cdata (eg. \n\n)

Reproduce code:
---------------
<?php

$fn = tempnam( '/tmp', 'test' );
if( $fp = fopen( $fn, 'w' ) ) {
fwrite( $fp, "one;val1\ntwo;val2\n\nthree;val3\n" );
fclose( $fp );

if( $fp = fopen( $fn, 'r' ) ) {
$items = array( );
while( $d = fgetcsv( $fp, 4096, ';' ) ) {
$items[] = $d;
}
fclose( $fp );
var_dump( $items );
}
}

?>

Expected result:
----------------
$items-count of 4

Actual result:
--------------
$item-count of 2


------------------------------------------------------------------------


--
Edit this bug report at http://bugs.php.net/?id=27730&edit=1
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com