PHP fgetcsv Definition and usage
The PHP fgetcsv () function reads a line from the file pointer and resolves the CSV field.
Like PHP fgets (), the difference is that PHP 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: PHP fgetcsv () operation is binary safe from php 4.3.5.
Grammar
Fgetcsv (File,length,separator,enclosure) |
Parameters |
Describe |
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. |
Hints and notes
Note: Empty rows in a CSV file are returned as an array containing a single null field and will not be treated as an error.
Note: This function is sensitive to locale settings. Let's say LANG is set to en_US. UTF-8, a single-byte encoded file will receive a read error.
Note: You can activate the Auto_detect_line_endings runtime configuration option if you encounter a line terminator for a Macintosh file that is not recognized by PHP when reading a file.
Example 1
Copy CodeThe 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 to:
Array ([0] = George [1] = John [2] = Thomas [3] = USA)
Example 2
Copy CodeThe 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 to:
Array ([0] = George [1] = John [2] = Thomas [3] = + USA Array ([0] = James [1] = = Adrew [2] = = M Artin [3] = USA)
windows and Linux compatibility issues
There's a problem today. Fgetcsv processing of the Linux platform under the empty data generated initially thought the PHP version of the problem, in fact, and the version is not related to the development of the Windows colleagues are no problem, and their own laptop and server, and the use of Linux system colleagues have empty data problems
Google a bit
Setting area: Simplified Chinese, UTF-8 encoding
Copy CodeThe code is as follows:
SetLocale (Lc_all, ' ZH_CN. UTF-8 ');
http://www.bkjia.com/PHPjc/325433.html www.bkjia.com true http://www.bkjia.com/PHPjc/325433.html techarticle PHP fgetcsv defines and uses the PHP fgetcsv () function to read a line from the file pointer and parse the CSV field. Similar to PHP fgets (), the difference is that PHP fgetcsv () parses the read-in line and finds out ...