Home  >  Article  >  Backend Development  >  Detailed explanation of usage examples of fgetcsv() function in PHP

Detailed explanation of usage examples of fgetcsv() function in PHP

怪我咯
怪我咯Original
2017-07-10 17:12:042567browse

This article mainly introduces the usage of fgetcsv() function in php, and demonstrates the method of using the fgetcsv() function to operate csv format files in the form of an example, which has certain reference For reference value, friends in need can refer to the

fgetcsv() function to read a line from the file pointer and parse the CSV field.

Similar to fgets(), except that fgetcsv() parses the read line and finds the fields in CSV format, then returns an array containing these fields.

fgetcsv() returns FALSE on error, including when the end of file is encountered.

Note: As of PHP 4.3.5, the operation of fgetcsv() is binary safe.

Syntax

fgetcsv(file,length,separator,enclosure)
Parameters Description
file Required. Specifies the documents to be checked.
length

Optional. Specifies the maximum length of a line. Must be larger than the longest line in the CVS file.

This parameter is optional in PHP 5. Required before PHP 5.

If you omit (set to 0 in PHP 5.0.4 and later versions) this parameter, there is no limit to the length, but it may affect execution efficiency.

separator Optional. Set field delimiter (only one character allowed), default is comma.
enclosure

Optional. Set field wrapping character (only one character allowed), default value is double quotes.

This parameter was added in PHP 4.3.0.

#Note: Empty lines 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 locale settings. For example, if LANG is set to en_US.UTF-8, single-byte encoded files will have read errors.

Note: If you encounter that PHP cannot recognize the line ending characters of Macintosh files when reading files, you can activate the auto_detect_line_endings runtime configuration option.

fgetcsv() function example code is as follows:

<?php 
$file = fopen("contacts.csv","r"); 
print_r(fgetcsv($file)); 
fclose($file); 
?>

The above is the detailed content of Detailed explanation of usage examples of fgetcsv() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn