fgetcsv
fgetcsv
Gets line from file pointer and parse for CSV fields
**fgetcsv** resource $stream $length string $separator string $enclosure string $escape
Similar to except that
parses the line it reads for fields in
format and returns an array containing the fields
read.
fgets``fgetcsv
Opmerking: > The locale settings are taken into account by this function. For example, data encoded in certain one-byte encodings may be parsed incorrectly if is .
LC_CTYPE``en_US.UTF-8
stream
A valid file pointer to a file successfully opened by
, , or
.
fopen``popen``fsockopen
length
Must be greater than the longest line (in characters) to be found in
the CSV file (allowing for trailing line-end characters). Otherwise the
line is split in chunks of characters,
unless the split would occur inside an enclosure.
length
Omitting this parameter (or setting it to 0, or null in PHP 8.0.0 or later) the maximum line length is not limited, which is slightly slower.
separator
The parameter sets the field separator.
It must be a single byte character.
separator
enclosure
The parameter sets the field enclosure character.
It must be a single byte character.
enclosure
escape
The parameter sets the escape character.
It must be a single byte character or the empty string.
The empty string () disables the proprietary escape mechanism.
escape``""
Waarschuwing: > In the input stream, the character can always be escaped by doubling it inside a quoted string, resulting in a single character in the parsed result. The character works differently: If a sequence of and characters appear in the input, both characters will be present in the parsed result. So for the default parameters, a CVS line like will have the fields parsed as and , respectively.
enclosure``enclosure``escape``escape``enclosure``"a""b","c\"d"``a"b``c\"d
Waarschuwing: > As of PHP 8.4.0, depending on the default value of is deprecated. It needs to be provided explicitly either positionally or by the use of .
escapenamed arguments
Returns an indexed array containing the fields read on success, return.falseforfailure.
Opmerking: > A blank line in a CSV file will be returned as an array comprising a single field, and will not be treated as an error.
null
Throws a if
or
is not one byte long.
separator``enclosure
Throws a if
is not one byte long or the empty string.
escape
Voorbeeld: Read and print the entire contents of a CSV file
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
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);
}
?>
fputcsv``str_getcsv``SplFileObject::fgetcsv``SplFileObject::fputcsv``SplFileObject::setCsvControl``SplFileObject::getCsvControl``explode``file``pack