Funkce PHP fgetcsv() .
❮ Reference souborového systému PHPPříklad
Přečtěte si a vytiskněte jeden řádek z otevřeného souboru CSV:
<?php
$file = fopen("contacts.csv","r");
print_r(fgetcsv($file));
fclose($file);
?>
Definice a použití
Funkce fgetcsv() analyzuje řádek z otevřeného souboru a kontroluje pole CSV .
Tip: Podívejte se také na funkci fputcsv() .
Syntax
fgetcsv(file, length, separator, enclosure)
Hodnoty parametrů
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 "\\" |
Technické údaje
Návratová hodnota: | Pole s poli CSV při úspěchu, NULL, pokud je dodán neplatný soubor , FALSE u ostatních chyb a při EOF |
---|---|
Verze PHP: | 4,0+ |
Binární trezor: | Ano, v PHP 4.3.5 |
PHP Changelog: | PHP 5.3 - Přidán parametr escape |
Další příklady
Příklad
Čtení a výstup celého obsahu souboru CSV:
<?php
$file = fopen("contacts.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
❮ Reference souborového systému PHP