PHP 手冊
上一頁 |  | 下一頁

# fgetcsv

(PHP 3>= 3.0.8, PHP 4 , PHP 5)

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

## Description

array fgetcsv ( resource handle, int length [, string delimiter [, string enclosure]])

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 optional third delimiter parameter defaults as a comma, and the optional enclosure defaults as a double quotation mark. Both delimiter and enclosure are limited to one character. If either is more than one character, only the first character is used.

> 備注: The enclosure parameter was added in PHP 4.3.0.

The handle parameter must be a valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen().

The length parameter 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.

> 備注: A blank line in a CSV file will be returned as an array comprising a single null field, and will not be treated as an error.

示範 1.Read and print the entire contents of a CSV file

`<?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); ?>`

fgetcsv() has been binary safe since PHP 4.3.5

See also explode(), file(), and pack()

上一頁 | 最開頭 | 下一頁
fgetc | 上一層 | fgets