★ wanayoo — archive 1999 http://www.php.net/manual/function.fgetcsv.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Filesystem
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Filesystem
* basename
* chgrp
* chmod
* chown
* clearstatcache
* copy
* delete
* dirname
* diskfreespace
* fclose
* feof
* fgetc
* fgetcsv
* fgets
* fgetss
* file
* file_exists
* fileatime
* filectime
* filegroup
* fileinode
* filemtime
* fileowner
* fileperms
* filesize
* filetype
* flock
* fopen
* fpassthru
* fputs
* fread
* fscanf
* fseek
* fstat
* ftell
* ftruncate
* fwrite
* set_file_buffer
* is_dir
* is_executable
* is_file
* is_link
* is_readable
* is_writeable
* link
* linkinfo
* mkdir
* pclose
* popen
* readfile
* readlink
* rename
* rewind
* rmdir
* stat
* lstat
* realpath
* symlink
* tempnam
* touch
* umask
* unlink
Manual: fgetcsv
View the source code for this pageSearch the site



Previous page
 fgetc
 Updated
Sat, 12 Aug 2000
fgets 
Next page


fgetcsv

(PHP3 >= 3.0.8, PHP4 )

fgetcsv --  Get line from file pointer and parse for CSV fields

Description

array fgetcsv (int fp, int length [, string delimiter])

Similar to fgets() except that fgetcsv() parses the line it reads for fields in CSV format and returns an array containing the fields read. The field delimiter is a comma, unless you specifiy another delimiter with the optional third parameter.

fp must be a valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen()

length must be greater than the longest line to be found in the CSV file (allowing for trailing line-end characters).

fgetcsv() returns false on error, including end of file.

NB A blank line in a CSV file will be returned as an array comprising just one single null field, and will not be treated as an error.

Example 1. Fgetcsv() example - Read and print entire contents of a CSV file


$row = 1;
$fp = fopen ("test.csv","r");
while ($data = fgetcsv ($fp, 1000, ",")) {
    $num = count ($data);
    print "<p> $num fields in line $row: <br>";
    $row++;
    for ($c=0; $c<$num; $c++) {
        print $data[$c] . "<br>";
    }
}
fclose ($fp);
     

User Contributed Notes: fgetcsv


quito@puc.cl
09-Feb-2000 05:15
I have a problem with this function "fgetcsv".
I can do: fgetcsv(int fp, int lengt), but if I try to use delimiter parameter the
php3 said that I have a count parameter error. Just it is succesful with two
first parameters.

Does Somebody knows a solution?

quito.



cleong@organic.com
24-Mar-2000 05:40
Methink the third parameter was added in a later version. I tried calling the function with three parameter in PHP/3.0.15 and it worked fine.


poc@baden-online.de
04-Apr-2000 07:36
fgetcsv() seems to ignore zero-length fields when working with tabs as separators (3.0.12).
Instead work around this with fgets() and do an explode() afterwards, this gets you the right thing.



gsims@pavtech.co.nz
18-Jul-2000 03:05
fgetcsv will break a field enclosed in quotes, if it contains the delimitter. Is their any way to escape the delimitter if you want it as part of the field?


gsims@pavtech.co.nz
18-Jul-2000 03:08
Oops. Ignore that last note, re Quoted fields. I was wrong. My code was screwed up.


 About Notes


Previous page
 fgetc
 Updated
Sat, 12 Aug 2000
fgets 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.