目录

PHP fputcsv() 函数

❮ PHP 文件系统参考

示例

将一行格式化为 CSV 并将其写入打开的文件:

<?php
$list = array (
  array("Peter", "Griffin" ,"Oslo", "Norway"),
  array("Glenn", "Quagmire", "Oslo", "Norway")
);

$file = fopen("contacts.csv","w");

foreach ($list as $line) {
  fputcsv($file, $line);
}

fclose($file);
?>


定义和用法

fputcsv() 函数将一行格式化为 CSV 并将其写入打开的文件。

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

语法

fputcsv( file, fields, separator, enclosure, escape)

参数值

Parameter Description
file Required. Specifies the open file to write to
fields Required. Specifies which array to get the data from
separator Optional. A character that specifies the field separator. Default is comma ( , )
enclosure Optional. A character that specifies the field enclosure character. Default is "
escape Optional. Specifies the escape character. Default is "\\". Can also be an empty string ("") which disables the escape mechanism


技术细节

返回值: 成功时写入字符串的长度,失败时写入 FALSE
PHP 版本: 5.1+
PHP 变更日志: PHP 7.4 - 的逃脱参数现在接受空字符串以禁用转义机制
PHP 5.5 - 添加了逃脱范围

❮ PHP 文件系统参考