PHP fgetcsv () function syntax parameters enclosure detailed

Source: Internet
Author: User
The Fgetcsv () function reads a row from the file pointer and resolves the CSV field.

Similar to Fgets (), the difference is that fgetcsv () parses a read-in row and finds a field in CSV format, and then returns an array containing those fields.

Fgetcsv () returns FALSE when an error occurs, including when the file ends.

Note: from PHP 4.3.5, the operation of Fgetcsv () is binary safe.

Grammar

Fgetcsv (File,length,separator,enclosure)
Parameters Description
File Necessary. Specifies the documents to be inspected.
Length

Optional. Specifies the maximum length of the line. Must be greater than the longest line within the CVS file.

This parameter is optional in PHP 5. is required prior to PHP 5.

If this parameter is omitted (set to 0 in a later version of PHP 5.0.4), then there is no limit to the length, but it may affect execution efficiency.

Separator Optional. Set the field delimiter (one character only), and the default value is a comma.
Enclosure

Optional. Sets the field wrapping character (only one character is allowed), and the default value is double quotation marks.

This parameter is added in PHP 4.3.0.

The explanation for enclosure is the character that surrounds the field.

Fields that contain commas, double quotes, or line breaks must be enclosed in quotation marks (special handling is required for only three special values).
The quotation marks inside the field must be preceded by a quotation mark to achieve the transcoding of the quotation marks.
Spaces before and after the delimiter comma may not be trimmed off.
The line break in the element is preserved.

Corresponding to the following example, it will be easier to understand the above explanation.

Field 1 Field 2 Field 3

Goog veture "Vision,good" ABC

For the three fields on the line above, if I want to use FGETSCV () to read it out to the array, the format stored in the CSV file must be qualified.

The contents of the CSV store are in the L two cases:

(1) Situation one, Direct is Goog,veture "Vision,good", ABC

This way you will find that get out is not what you want. Its array output is

Array ([0] = goog [1] = Vetur "evision [2] = = Good" [3] = ABC)

(2) In case two, the content in CSV is GOOG, "veture" "Vision,good" "", ABC

This is based on the above description of the original content has been modified. Plus the Fu Di quotes around the word.

Its array output is what we want.

Array ([0] = goog [1] = veture "Vision,good" [2] = = ABC)

For some of the above instructions, if the field contains a comma, just enclose the field in double quotation marks, without having to precede the comma with double quotes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.