Home  >  Article  >  Backend Development  >  php fgetcsv function_PHP tutorial

php fgetcsv function_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:01:101086browse

php fgetcsv function ​

fgetcsv
(PHP 4, PHP 5)

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

Description
Array fgetcsv (resource$process[summary$length[, string$delimiter[, string$enclosed[, string$escape]]]])
Similar to fgets(), but fgetcsv() parses the rows of fields read in CSV format and returns an array containing the fields read.

Parameters

Handle
A valid file pointer to the file successfully opened by fopen(), popen() or fsockopen().

Length
Must be larger than the longest line (in characters) that can be found in the CSV file (trailing line-end characters are allowed). It became optional in PHP 5. Omitting this parameter (or setting it to 0 in PHP 5.0.4 and later) makes the maximum line length unlimited, which is slightly slower.

Delimiter
Set field delimiter (only one character). The default value is a comma.

Text
Set of foreign characters (one character only). Defaults to double quotes.

Escape
Set the escape character (one character only). The default value is backslash ( )


Return value
Returns an array containing the indexed fields read.

NOTE: A blank line in the CSV file will be returned as an array consisting of a single empty field and will not be considered an error.


Note: If PHP does not correctly recognize line endings when either reading a file or creating it on a Macintosh, enabling the auto_detect_line_endings runtime configuration option may help resolve this issue.

fgetcsv() returns FALSE on errors, including end-of-file.

Modify

Release Notes
5.3.0 jailbreak parameters added
4.3.5 fgetcsv() is now binary safe
4.3.0 Attachment parameters added


Example

Example #1 Read and print the entire contents of a CSV file

$row = 1;
$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "

$num fields in line $row:

n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "
n";
}
}
fclose($handle);
?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445476.htmlTechArticlephp fgetcsv function fgetcsv (PHP 4, PHP 5) fgetcsv - Get line pointer from file and parse to CSV field description array fgetcsv(resource$process[summary$length[, string$delimited...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn