将一行格式化为 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 - 添加了逃脱范围 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!