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 หากระบุไฟล์ ที่ไม่ถูกต้อง FALSE จากข้อผิดพลาดอื่นๆ และใน EOF |
---|---|
รุ่น PHP: | 4.0+ |
ตู้เซฟไบนารี: | ใช่ ใน PHP 4.3.5 |
บันทึกการเปลี่ยนแปลง PHP: | PHP 5.3 - เพิ่มEscape Parameter |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
อ่านและส่งออกเนื้อหาทั้งหมดของไฟล์ CSV:
<?php
$file = fopen("contacts.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
❮ การอ้างอิงระบบไฟล์ PHP