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

怪我咯
Release: 2023-03-13 13:12:01
Original
2580 people have browsed it

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)
Copy after login
ParametersDescription
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.

separatorOptional. 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); 
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template