Interpretation of parameter Enclosure in PHP function Fgetcsv ()

Source: Internet
Author: User

Prototype Fgetcsv (resource FP, int length [, String delimiter [, String enclosure [, String escape]])

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

Refer to the http://blog.ifeeline.com/217.html description:

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.

In addition, for the last parameter escape, the official explanation is to set the escape character. I do not understand this, I hope someone can provide an example to explain.

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.