Fgetcsv
(PHP 4 and PHP 5)
Fgetcsv-get the line from the file pointer and parsed to the CSV field
Description
Array fgetcsv (resource $ processing [Abstract $ length [, string $ delimiter [, string $ 文[ [, string $ escape])
Similar to fgets (), the fields read by the fgetcsv () parsing row are in CSV format and an array containing fields is returned for reading.
Parameters
Handle
A valid file pointer to the file successfully opens the fopen () function, popen () or fsockopen ().
Length
It must be greater than the CSV file that can be found in the longest line (in characters) (The End character of the trailing line is allowed ). It becomes optional in PHP 5. The maximum length of this parameter (or set to 0 in PHP 5.0.4 or later) is slightly slowed down.
Delimiters
Set field separator (only one character ). The default value is a comma.
Text
Set foreign characters (only one character ). The default value is double quotation marks.
Escape
Set the escape character (only one character ). The default value is the backslash ()
Return value
Returns an array containing index fields for reading.
Note: a CSV file with blank rows will return an array to form a single blank field and will not be considered as an error.
Note: If PHP does not correctly recognize the end of a line, whether it is reading a file or creating a Macintosh computer, enabling the auto_detect_line_endings runtime configuration option may help solve this problem.
Fgetcsv () returns a FALSE error, including the end of the file.
Modify
Version description
5.3.0 jailbreak parameter added
4.3.5 fgetcsv () is currently binary secure
4.3.0 attachment parameter added
Instance
For example, #1 reads and prints all CSV files.
<? Php
$ Row = 1;
$ 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 );
?>