Hàm fgetcsv () trong PHP
❮ Tham chiếu hệ thống tệp PHPThí dụ
Đọc và xuất một dòng từ tệp CSV đang mở:
<?php
$file = fopen("contacts.csv","r");
print_r(fgetcsv($file));
fclose($file);
?>
Định nghĩa và Cách sử dụng
Hàm fgetcsv () phân tích cú pháp một dòng từ tệp đang mở, kiểm tra các trường CSV .
Mẹo: Cũng xem hàm fputcsv () .
Cú pháp
fgetcsv(file, length, separator, enclosure)
Giá trị tham số
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 "\\" |
Chi tiết kỹ thuật
Giá trị trả lại: | Một mảng có các trường CSV thành công, NULL nếu tệp không hợp lệ được cung cấp, FALSE đối với các lỗi khác và trên EOF |
---|---|
Phiên bản PHP: | 4.0+ |
Két an toàn nhị phân: | Có, trong PHP 4.3.5 |
PHP Changelog: | PHP 5.3 - Đã thêm tham số thoát |
Các ví dụ khác
Thí dụ
Đọc và xuất toàn bộ nội dung của tệp CSV:
<?php
$file = fopen("contacts.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
❮ Tham chiếu hệ thống tệp PHP