0

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.

Paramaters

ParameterDescription
handleA valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen().
lengthMust be greater than the longest line.
delimiterSet the field delimiter (one character only). Defaults as a comma.
enclosureSet the field enclosure character (one character only). Defaults as a double quotation mark.
escapeSet 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);
?>

Post a Comment

 
Top