Función PHP fgetcsv()
❮ Referencia del sistema de archivos PHPEjemplo
Lea y genere una línea del archivo CSV abierto:
<?php
$file = fopen("contacts.csv","r");
print_r(fgetcsv($file));
fclose($file);
?>
Definición y uso
La función fgetcsv() analiza una línea de un archivo abierto y busca campos CSV .
Sugerencia: vea también la función fputcsv() .
Sintaxis
fgetcsv(file, length, separator, enclosure)
Valores paramétricos
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 "\\" |
Detalles técnicos
Valor devuelto: | Una matriz con los campos CSV en caso de éxito, NULL si se proporciona un archivo no válido , FALSE en otros errores y en EOF |
---|---|
Versión PHP: | 4.0+ |
Caja fuerte binaria: | Sí, en PHP 4.3.5 |
Registro de cambios de PHP: | PHP 5.3 - Se agregó el parámetro de escape |
Más ejemplos
Ejemplo
Leer y generar todo el contenido de un archivo CSV:
<?php
$file = fopen("contacts.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
❮ Referencia del sistema de archivos PHP