Phpfgetcsv reads csv file code

Source: Internet
Author: User
? Phpfunctionget_csv_contents ($ file_target) {$ handlenbs...
  $num fields in line $row: 
"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c]. "
";; /*echo getUTFString($data[$c])*/ } } fclose($handle); }?>

Array fgetcsv (int handle [, int length [, string delimiter [, string enclosure])

Handle

A valid file pointer generated by fopen (), popen (), or fsockopen.

Length (optional)

This parameter must be greater than the longest line in the CVS file. in PHP 5, this parameter is optional. If this parameter is ignored (set to 0 in PHP 5.0.4 and later versions, there is no limit on the length, but it may affect the execution efficiency.

Delimiter (optional)

Set the field delimiter (only one character is allowed). The default value is comma.

Enclosure (optional)

Set the field surround character (only one character is allowed). The default value is double quotation marks. this parameter is added in PHP 4.3.0. similar to fgets (), only the rows read by fgetcsv () are parsed, fields in CSV format are found, and an array containing these fields is returned.

If an error occurs in fgetcsv (), FALSE is returned, including when the file ends.

Note: empty rows in the CSV file will be returned as an array containing a single null field and will not be treated as an error.

Example 1: read and display the entire content of the CSV file. the code is as follows:

$row = 1; $handle = fopen("test.csv","r"); while ($data = fgetcsv($handle, 1000, ",")) { $num = count($data);echo "

$num fields in line $row:
"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "
"; } } fclose($handle);


Permanent link:

Reprint at will! Include the article address.

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.