Hướng dẫn dùng fputcsv trong PHP

  • Hàm fputcsv [] trong PHP
      • Ví dụ
    • Định nghĩa và Cách sử dụng
    • Cú pháp
    • Giá trị tham số
    • Chi tiết kỹ thuật
      • Related posts:

❮ Tham chiếu hệ thống tệp PHP

Ví dụ

Định dạng một dòng dưới dạng CSV và ghi nó vào một tệp đang mở:

Định nghĩa và Cách sử dụng

Hàm fputcsv [] định dạng một dòng dưới dạng CSV và ghi nó vào một tệp đang mở.

Mẹo: Cũng xem hàm fgetcsv [] .

Cú pháp

fputcsv[file, fields, separator, enclosure,
escape]

Giá trị tham số

ParameterDescription
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

Chi tiết kỹ thuật

Giá trị trả lại:Phiên bản PHP:PHP Changelog:
Độ dài của chuỗi được viết khi thành công, FALSE khi thất bại
5.1+
PHP 7.4 – Tham số thoát bây giờ chấp nhận một chuỗi trống để vô hiệu hóa cơ chế thoát
PHP 5.5 – Đã thêm tham số thoát

❮ Tham chiếu hệ thống tệp PHP

botvietbai.com

BCK là giải pháp tích hợp thanh toán MOMO, ViettelPay, VNPay, Vietcombank, Vietinbank, Techcombank, MB.. dành cho cá nhân !

[PHP 5 >= 5.1.0, PHP 7, PHP 8]

fputcsvFormat line as CSV and write to file pointer

Description

fputcsv[
    resource $stream,
    array $fields,
    string $separator = ",",
    string $enclosure = "\"",
    string $escape = "\\",
    string $eol = "\n"
]: int|false

Parameters

stream

The file pointer must be valid, and must point to a file successfully opened by fopen[] or fsockopen[] [and not yet closed by fclose[]].

fields

An array of strings.

separator

The optional separator parameter sets the field delimiter [one single-byte character only].

enclosure

The optional enclosure parameter sets the field enclosure [one single-byte character only].

escape

The optional escape parameter sets the escape character [at most one single-byte character]. An empty string [""] disables the proprietary escape mechanism.

eol

The optional eol parameter sets a custom End of Line sequence.

Note:

If an enclosure character is contained in a field, it will be escaped by doubling it, unless it is immediately preceded by an escape.

Return Values

Returns the length of the written string or false on failure.

Changelog

VersionDescription
8.1.0 The optional eol parameter has been added.
7.4.0 The escape parameter now also accepts an empty string to disable the proprietary escape mechanism.

Examples

Example #1 fputcsv[] example

Chủ Đề