The fgetcsv() function parses a line from an open file and verifies the CSV fields .
The fgetcsv() function stops returning a new line when it reaches the specified length or reaches the end of file (EOF), whichever comes first.
This function returns the CSV fields as an array if successful, or FALSE if it fails or the end of file (EOF) is reached.
fgetcsv(file,length,separator,enclosure)
parameter | describe |
---|---|
file | Required. Specifies the documents to be checked. |
length | Optional. Specifies the maximum length of a line. Must be larger than the longest line in the CSV file. If this parameter is omitted (or set to 0), there is no limit on the line length, but execution efficiency may be affected. Note: This parameter is required in versions prior to PHP 5. |
separator | Optional. Set field delimiter (only one character allowed), default value is comma (,). |
enclosure | Optional. Set field wrap character (only one character allowed), default is double quotes ( " ). |
Tip: See the fputcsv() function.
<?php$file = fopen("contacts.csv","r");print_r(fgetcsv($file));fclose($file);?>
CSV file:
Kai Jim, Refsnes, Stavanger, NorwayHege, Refsnes, Stavanger, Norway
The above code will output:
Array([0] => Kai Jim[1] => Refsnes[2] => Stavanger[3] => Norway)
<?php$file = fopen("contacts.csv","r");while(! feof($file)) { print_r(fgetcsv($file)); }fclose($file);?>
CSV file:
Kai Jim, Refsnes, Stavanger, NorwayHege, Refsnes, Stavanger, Norway
The above code will output:
Array([0] => Kai Jim[1] => Refsnes[2] => Stavanger[3] => Norway)Array([0] => Hege[1] => Refsnes[2] => Stavanger[3 ] => Norway)