This article mainly introduces the Fgetcsv () function usage in PHP, demonstrates the method of using Fgetcsv () function to manipulate CSV format file in the instance form, has certain reference value, the need friend can refer to the following
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. |
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.
The Fgetcsv () function instance code is as follows:
<?php $file = fopen ("Contacts.csv", "R"), Print_r (Fgetcsv ($file)); Fclose ($ file);