Learning PHP
Advanced PHP
PHP Function Reference
PHP Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM
|
PHP Function fgetcsv()
Syntax
array fgetcsv ( resource $handle [, int $length [, string $delimiter [, string $enclosure [, string $escape]]]] );
|
Definition and Usage
Similar to fgets() except that fgetcsv() parses the line it reads for fields in CSV format and returns an array containing the fields read.
Paramters
Parameter | Description |
handle | A valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen(). |
length | Must be greater than the longest line. |
delimiter | Set the field delimiter (one character only). Defaults as a comma. |
enclosure | Set the field enclosure character (one character only). Defaults as a double quotation mark. |
escape | Set the escape character (one character only). Defaults as a backslash (\) |
Return Value
Returns an indexed array containing the fields read.
Example
Following is the usage of this function:
<?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);
?>
|
|
|
|