Learn a new php function (2) fgetcsv ()/fgets (), fgetcsvfgets

Source: Internet
Author: User

Learn a new php function (2) fgetcsv ()/fgets (), fgetcsvfgets

Reading and Writing files is a common action. There are thousands of functions for reading files, which are complex and simple. The most common and convenient operations are file_get_contents () and file_put_conents (). You do not need to open or close a file.
But when reading large files, file_get_contents () will read all the content into the memory, resulting in memory overflow. It is best to read it cyclically by row. Fgetcsv () is used to read a single csv file, and fgets () is used to read the same common file.

Fgetcsv ()

Csv is a form text file in special format. Each field is separated by ',', and each line is separated by '\ n. You can use fgetcsv to read each field, or use fgets to read each row, and then use explode (',', $ data) to separate data in each row.
Fgetcsv-read a row from the file pointer and parse the CSV Field

Array fgetcsv (resource $ handle [, int $ length = 0 [, string $ delimiter = ',' [, string $ enclosure = '"'[, string $ escape = '\'])

Parameters

Handle
Accept a valid file pointer generated by fopen (), popen (), or fsockopen.
Length
It must be greater than the maximum row in the CVS file. This parameter is optional in 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.
Delimiter
Set the field delimiter (only one character is allowed ).
Enclosure
Set the field surround character (only one character is allowed ).
Escape
Set the Escape Character (only one character is allowed), which is a backslash by default.
Note: If the length of the csv field is greater than the set length, the function reads only the characters with the length at a time and returns an array with the index of 0. When the separator ',' is encountered, the character string after the separator is blocked.
Example

<?phpfile_put_contents('test.csv','user1111,user222'."\n".'user3333,user4444');if (($handle = fopen("test.csv", "r")) !== FALSE) {    while (($data = fgetcsv($handle, 6, ",")) !== FALSE) {        echo $data[0] . "<br />\n";    }    fclose($handle);}?> 

The above will be output

User11
11
R222
User33
33
R4444

Fgets ()

Fgets-read a row from the file pointer

string fgets ( resource $handle [, int $length ] )
Parameters

Handle
The file pointer must be valid and must be directed to a file successfully opened by fopen () or fsockopen () (not closed by fclose ).

Length
Read a row from the file pointed to by handle and return a string of up to length-1 bytes. When a line break (including the returned value), EOF, or the length-1 byte is read, it is stopped ). If length is not specified, the default value is 1 K, or 1024 bytes.

Note:

When the length is ignored from PHP 4.3, the row length is assumed to be 1024, and the data is read from the stream until the row ends. If most of the rows in the file are larger than 8 KB, it is more effective to specify the length of the maximum row in the script to use resources.

Return Value

Read the length-1 byte from the file pointed to by the pointer handle and then return the string. If no more data exists in the file pointer, FALSE is returned.

If an error occurs, FALSE is returned.

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.