PHP -fgetcsv()- Funktion
❮ Referenz zum PHP-DateisystemBeispiel
Eine Zeile aus der geöffneten CSV-Datei lesen und ausgeben:
<?php
$file = fopen("contacts.csv","r");
print_r(fgetcsv($file));
fclose($file);
?>
Definition und Verwendung
Die Funktion fgetcsv() parst eine Zeile aus einer geöffneten Datei und prüft auf CSV-Felder .
Tipp: Siehe auch die Funktion fputcsv() .
Syntax
fgetcsv(file, length, separator, enclosure)
Parameterwerte
Parameter | Description |
---|---|
file | Required. Specifies the open file to return and parse a line from |
length | Optional. Specifies the maximum length of a line. Must be greater than the longest line (in characters) in the CSV file. Omitting this parameter (or setting it to 0) the line length is not limited, which is slightly slower. Note: This parameter is required in versions prior to PHP 5 |
separator | Optional. Specifies the field separator. Default is comma ( , ) |
enclosure | Optional. Specifies the field enclosure character. Default is " |
escape | Optional. Specifies the escape character. Default is "\\" |
Technische Details
Rückgabewert: | Ein Array mit den CSV-Feldern bei Erfolg, NULL, wenn eine ungültige Datei bereitgestellt wird, FALSE bei anderen Fehlern und bei EOF |
---|---|
PHP-Version: | 4.0+ |
Binär sicher: | Ja, in PHP 4.3.5 |
PHP-Änderungsprotokoll: | PHP 5.3 - Escape -Parameter hinzugefügt |
Mehr Beispiele
Beispiel
Den gesamten Inhalt einer CSV-Datei lesen und ausgeben:
<?php
$file = fopen("contacts.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
❮ Referenz zum PHP-Dateisystem