目录

PHP fgetcsv() 函数

❮ PHP 文件系统参考

示例

从打开的 CSV 文件中读取并输出一行:

<?php
$file = fopen("contacts.csv","r");
print_r(fgetcsv($file));
fclose($file);
?>
运行示例 »

定义和用法

fgetcsv() 函数从打开的文件中解析一行,检查CSV 字段

提示:另请参阅fputcsv()功能。

语法

fgetcsv( file, length, separator, enclosure)

参数值

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 "\\"


技术细节

返回值: 成功时包含 CSV 字段的数组,如果无效则为 NULL文件已提供,其他错误和 EOF 时为 FALSE
PHP 版本: 4.0+
二进制安全: 是的,在 PHP 4.3.5 中
PHP 变更日志: PHP 5.3 - 添加了逃脱范围

更多示例

示例

读取并输出 CSV 文件的全部内容:

<?php
$file = fopen("contacts.csv","r");

while(! feof($file))
  {
  print_r(fgetcsv($file));
  }

fclose($file);
?>
运行示例 »

❮ PHP 文件系统参考