PHPfgetcsv definition and usage (with compatibility issues in windows and linux) _ PHP Tutorial

Source: Internet
Author: User
PHPfgetcsv definition and usage (with compatibility issues between windows and linux ). PHPfgetcsv definition and usage the PHPfgetcsv () function reads a row from the file pointer and parses the CSV field. Similar to PHPfgets (), the difference is that PHPfgetcsv () parses the rows read and finds out the definition and usage of PHP fgetcsv.

The PHP fgetcsv () function reads a row from the file pointer and parses the CSV field.
Similar to PHP fgets (), the difference is that PHP fgetcsv () parses the rows read, finds fields in CSV format, and returns an array containing these fields.

If an error occurs in fgetcsv (), FALSE is returned, including when the file ends.

Note: from PHP 4.3.5, PHP fgetcsv () operations are binary safe.

Syntax

Fgetcsv (file, length, separator, enclosure)
Parameters Description
File Required. Specifies the file to be checked.
Length Optional. Specifies the maximum length of a row. It must be greater than the maximum row in the CVS file.
This parameter is optional in PHP 5. Required before PHP 5.
If this parameter is ignored (set to 0 in PHP 5.0.4 and later versions), the length is not limited, but the execution efficiency may be affected.
Separator Optional. Set the field delimiter (only one character is allowed). The default value is comma.
Enclosure Optional. Set the field surround character (only one character is allowed). The default value is double quotation marks.
This parameter is added in PHP 4.3.0.
Tips and comments

Note: empty rows in the CSV file will be returned as an array containing a single null field and will not be treated as an error.
Note: This function is sensitive to region settings. For example, if LANG is set to a en_US.UTF-8, a single-byte encoded file will have a read error.
Note: If PHP cannot recognize the row Terminator of the Macintosh File when reading the file, you can activate the auto_detect_line_endings runtime configuration option.

Example 1

The code is as follows:


$ File = fopen ("contacts.csv", "r ");
Print_r (fgetcsv ($ file ));
Fclose ($ file );
?>



CSV file:
George, John, Thomas, USA James, Adrew, Martin, USA
The output is similar:
Array ([0] => George [1] => John [2] => Thomas [3] => USA)

Example 2

The code is as follows:


   $ File = fopen ("contacts.csv", "r ");
While (! Feof ($ file) {print_r (fgetcsv ($ file ));
} Fclose ($ file );
?>


CSV file:
George, John, Thomas, USA James, Adrew, Martin, USA
The output is similar:
Array ([0] => George [1] => John [2] => Thomas [3] => USA Array ([0] => James [1] => Adrew [2] => Martin [3] => USA)

Compatibility issues in windows and linux

I reported a problem today that the free data processed by fgetcsv on the linux platform was generated and thought it was a php version problem at first. In fact, it is not related to the version, and the development colleagues in the window are not correct, however, my colleagues in the local and server, and linux system, all experienced the problem of empty data.

Google

Set area: Simplified Chinese, UTF-8 encoding

The code is as follows:


Setlocale (LC_ALL, 'zh _ CN.UTF-8 ');

The http://www.bkjia.com/PHPjc/325433.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/325433.htmlTechArticlePHP fgetcsv definition and usage PHP fgetcsv () function reads a row from the file pointer and parses the CSV field. Similar to PHP fgets (), the difference is that PHP fgetcsv () parses the rows read and finds out...

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.