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 - ตอนนี้พารามิเตอร์ Escapeยอมรับสตริงว่างเพื่อปิดใช้งานกลไก Escape PHP 5.5 - เพิ่มพารามิเตอร์Escape |
❮ การอ้างอิงระบบไฟล์ PHP