One day learning a new PHP function (2) fgetcsv ()/fgets ()

Source: Internet
Author: User
File read and write is a frequent action, read the function of the file is thousands of, complex, simple also has. Most commonly used conveniently have file_get_contents (), file_put_conents (), do not need to open the file, close the operation of the file.
However, when a large file is read, file_get_contents () reads the contents into memory, causing the memory to overflow, preferably a loop to read by line. Fgetcsv () is used to read a row of CSV files, and fgets () is used to read common files.

Fgetcsv ()

CSV, is a form text file in a special format that separates each field with ', ', separating each line with ' \ n '. You can read each field with Fgetcsv, or read each row with fgets, and then separate the data for each row with explode (', ', $data).
Fgetcsv? Reads a row from the file pointer and resolves the CSV field

Array Fgetcsv (Resource $handle [, int $length = 0 [, String $delimiter = ', ' [, String $enclosure = ' "' [, String $escap e = ' \ ']]])

Parameters

Handle
Accepts a valid file pointer produced by fopen (), Popen (), or fsockopen ().
Length
Must be greater than the longest line within the CVS file. This parameter is optional in 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.
Delimiter
Sets the field delimiter (only one character is allowed).
Enclosure
Set the field wrapping character (only one characters allowed).
Escape
Sets the escape character (only one character is allowed), and the default is a backslash.
Note: If the characters in the CSV field are longer than the length set, the function will read only length characters and return an array with an index of 0, and when the delimiter ', ' is encountered, the string after the delimiter is masked.
Example

 
  \ n ";    }    

The above will output

User11
11
r222
User33
33
r4444

Fgets ()

Fgets? Reads a row from the file pointer

String fgets (Resource $handle [, int $length])

Parameters

Handle
The file pointer must be valid and must point to a file that was successfully opened by fopen () or Fsockopen () (and not yet closed by fclose ()).

Length
Reads a row from the file pointed to by handle and returns a string of up to length-1 bytes in length. Stop after encountering a newline character (included in the return value), EOF, or having read the length-1 byte (see first the case). If length is not specified, the default is 1 K, or 1024 bytes.

Note:

starting with PHP 4.3, ignoring the length of the line is assumed to be 1024, and the data continues to be read from the stream until the end of the line. If most of the rows in the file are larger than 8KB, specifying the length of the largest row in the script is more efficient with resources.

return value

Returns a string after reading the length-1 byte from the file pointed to by the pointer handle. Returns FALSE if there is no more data in the file pointer.

Returns False when an error occurs.

  • 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.