Funzione PHP fgetcsv()
❮ Riferimento al file system PHPEsempio
Leggi e genera una riga dal file CSV aperto:
<?php
$file = fopen("contacts.csv","r");
print_r(fgetcsv($file));
fclose($file);
?>
Definizione e utilizzo
La funzione fgetcsv() analizza una riga da un file aperto, controllando i campi CSV .
Suggerimento: vedere anche la funzione fputcsv() .
Sintassi
fgetcsv(file, length, separator, enclosure)
Valori dei parametri
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 "\\" |
Dettagli tecnici
Valore di ritorno: | Un array con i campi CSV in caso di successo, NULL se viene fornito un file non valido, FALSE su altri errori e su EOF |
---|---|
Versione PHP: | 4.0+ |
Cassaforte binaria: | Sì, in PHP 4.3.5 |
Registro delle modifiche PHP: | PHP 5.3 - Aggiunto il parametro escape |
Altri esempi
Esempio
Leggi e genera l'intero contenuto di un file CSV:
<?php
$file = fopen("contacts.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
❮ Riferimento al file system PHP