Hàm fgetcsv() của PHP
❮ Tham khảo hệ thống tập tin PHPVí 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);
?>
Chạy ví dụ »Định nghĩa và cách sử dụng
Hàm fgetcsv() phân tích một dòng từ một tệp đang mở, kiểm tra các trường CSV .
Mẹo: Xem thêm 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ả về: | Một mảng có các trường CSV thành công, NULL nếu cung cấp tệp không hợp lệ, FALSE đối với các lỗi khác và trên EOF |
---|---|
Phiên bản PHP: | 4.0+ |
An toàn nhị phân: | Có, trong PHP 4.3.5 |
Nhật ký thay đổi PHP: | PHP 5.3 - Đã thêm tham số thoát |
Thêm ví dụ
Ví 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);
?>
Chạy ví dụ »❮ Tham khảo hệ thống tập tin PHP