Fungsi PHP fputcsv()
Referensi Sistem File PHPContoh
Format baris sebagai CSV dan tulis ke file yang terbuka:
<?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);
?>
Definisi dan Penggunaan
Fungsi fputcsv() memformat garis sebagai CSV dan menulisnya ke file yang terbuka.
Tip: Lihat juga fungsi fgetcsv() .
Sintaksis
fputcsv(file, fields, separator, enclosure,
escape)
Nilai Parameter
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 |
Detail Teknis
Nilai Kembali: | Panjang string tertulis pada keberhasilan, SALAH pada kegagalan |
---|---|
Versi PHP: | 5.1+ |
Log Perubahan PHP: | PHP 7.4 - Parameter escape sekarang menerima string kosong untuk menonaktifkan mekanisme escape PHP 5.5 - Menambahkan parameter escape |
Referensi Sistem File PHP