Função PHP fgetcsv()
❮ Referência do sistema de arquivos PHPExemplo
Leia e imprima uma linha do arquivo CSV aberto:
<?php
$file = fopen("contacts.csv","r");
print_r(fgetcsv($file));
fclose($file);
?>
Definição e uso
A função fgetcsv() analisa uma linha de um arquivo aberto, verificando os campos CSV .
Dica: veja também a função fputcsv() .
Sintaxe
fgetcsv(file, length, separator, enclosure)
Valores de parâmetro
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 "\\" |
Detalhes técnicos
Valor de retorno: | Uma matriz com os campos CSV em caso de sucesso, NULL se um arquivo inválido for fornecido, FALSE em outros erros e em EOF |
---|---|
Versão do PHP: | 4.0+ |
Binário Seguro: | Sim, no PHP 4.3.5 |
Registro de alterações do PHP: | PHP 5.3 - Adicionado o parâmetro escape |
Mais exemplos
Exemplo
Leia e produza todo o conteúdo de um arquivo CSV:
<?php
$file = fopen("contacts.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
❮ Referência do sistema de arquivos PHP