PHP read CSV file content detailed

Source: Internet
Author: User
Introduction to the CSV file:

CSV is a common, relatively simple file format that is widely used by users, businesses, and science. The most widespread application is the transfer of tabular data between programs, which themselves operate in incompatible formats (often in private and/or non-canonical formats). Because a large number of programs support some kind of CSV variant, at least as an optional input/output format.

For example, a user might need to exchange information, from a database program that stores data in a private format, to a spreadsheet with a completely different data format. Most likely, the database program can export data as "CSV", and then the exported CSV file can be imported by a spreadsheet program.

    1. "CSV" is not a single, well-defined format (although RFC 4180 has a commonly used definition). In practice, therefore, the term "CSV" refers to any file that has the following characteristics:

    2. Plain text, using a character set such as ASCII, Unicode, EBCDIC, or GB2312;

    3. Consists of records (typically one record per line);

    4. Each record is delimited by a delimiter as a field (a typical delimiter has commas, semicolons, or tabs; sometimes separators can include optional spaces);

    5. Each record has the same field sequence.

There are many CSV variants under these regular constraints, so CSV files are not completely interoperable. However, these mutations are very small and there are many applications that allow the user to preview the file (which is possible because it is plain text) and then specify delimiters, escape rules, and so on. If a particular CSV file is too large to mutate beyond the support of a particular receiving program, it is often advisable to manually check and edit the file, or to fix the problem with a simple program. Therefore, in practice, the CSV file is very convenient.

This article is a detailed analysis of the contents of PHP read CSV file, need to refer to the friend

Read data from all rows in a CSV file at once

<?php $file = fopen (' windows_2011_s.csv ', ' R '); while ($data = Fgetcsv ($file)) {//reads a single line of content//print_r ($data) per CSV;//This is an array, to get each data, access the array subscript can $goods_list[] = $data;} Print_r ($goods _list);/* foreach ($goods _list as $arr) {    if ($arr [0]!= "") {        echo $arr [0]. " <br> ";    }} */Echo $goods _list[2][0]; Fclose ($file);? >

Reads a row of data from a CSV file

<?phpfunction Get_file_line ($file _name, $line) {  $n = 0;  $handle = fopen ($file _name, ' R ');  if ($handle) {    while (!feof ($handle)) {        + + $n;        $out = Fgets ($handle, 4096);        if ($line = = $n) break;    }    Fclose ($handle);  }  if ($line = = $n) return $out;  return false;} Echo get_file_line ("Windows_2011_s.csv", 10);? >

Read CSV file number of rows (line interval)

<?phpfunction get_file_line ($file _name, $line _star, $line _end) {$n = 0;    $handle = fopen ($file _name, "R");            if ($handle) {while (!feof ($handle)) {+ + $n;            $out = Fgets ($handle, 4096);            if ($line _star <= $n) {$ling [] = $out;        } if ($line _end = = $n) break;    } fclose ($handle);    } if ($line _end== $n) return $ling; return false;}  $AA = Get_file_line ("Windows_2011_s.csv", 11, 20); From line 11th to line 20th, foreach ($aa as $BB) {echo $bb. <br> ";}? 
Related Article

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.