Php fgetcsv function

Source: Internet
Author: User

Fgetcsv
(PHP 4 and PHP 5)

Fgetcsv-get the line from the file pointer and parsed to the CSV field

Description
Array fgetcsv (resource $ processing [Abstract $ length [, string $ delimiter [, string $ 文[ [, string $ escape])
Similar to fgets (), the fields read by the fgetcsv () parsing row are in CSV format and an array containing fields is returned for reading.

Parameters

Handle
A valid file pointer to the file successfully opens the fopen () function, popen () or fsockopen ().

Length
It must be greater than the CSV file that can be found in the longest line (in characters) (The End character of the trailing line is allowed ). It becomes optional in PHP 5. The maximum length of this parameter (or set to 0 in PHP 5.0.4 or later) is slightly slowed down.

Delimiters
Set field separator (only one character ). The default value is a comma.

Text
Set foreign characters (only one character ). The default value is double quotation marks.

Escape
Set the escape character (only one character ). The default value is the backslash ()


Return value
Returns an array containing index fields for reading.

Note: a CSV file with blank rows will return an array to form a single blank field and will not be considered as an error.


Note: If PHP does not correctly recognize the end of a line, whether it is reading a file or creating a Macintosh computer, enabling the auto_detect_line_endings runtime configuration option may help solve this problem.

Fgetcsv () returns a FALSE error, including the end of the file.

Modify

Version description
5.3.0 jailbreak parameter added
4.3.5 fgetcsv () is currently binary secure
4.3.0 attachment parameter added


Instance

For example, #1 reads and prints all CSV files.

<? Php
$ Row = 1;
$ Handle = fopen ("test.csv", "r ");
While ($ data = fgetcsv ($ handle, 1000 ,","))! = FALSE ){
$ Num = count ($ data );
Echo "<p> $ num fields in line $ row: <br/> </p> n ";
$ Row ++;
For ($ c = 0; $ c <$ num; $ c ++ ){
Echo $ data [$ c]. "<br/> n ";
    }
}
Fclose ($ handle );
?>

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.