Home  >  Article  >  Backend Development  >  php read CSV file example

php read CSV file example

怪我咯
怪我咯Original
2017-07-16 14:42:391686browse

CSV is a versatile, relatively simple file format widely used by users, business and science. The most widespread application is for transferring tabular data between programs that themselves operate on incompatible formats (often proprietary and/or non-canonical formats). Because a large number of programs support some variant of CSV, at least as an optional input/output format.

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

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

  • plain text, using a certain character set, such as ASCII, Unicode, EBCDIC or GB2312;

  • consists of records (typically one record per line);

  • Each record is separated into fields by a delimiter (Typical delimiters are commas, semicolons, or tabs; sometimes delimiters can include optional spaces);

  • Each record has the same field sequence.

Under these general constraints, there are many CSV variants, so CSV files are not completely interoperable. However, these variations are very minor, and there are many applications that allow the user to preview the file (which is possible since it is plain text) and then specify delimiters, escaping rules, etc. If the variation in a particular CSV file is too large to be supported by a particular receiving program, then the feasible approach is often to manually inspect and edit the file, or to fix the problem through a simple program. Therefore, in practice, CSV files are still very convenient.


This article mainly introduces the function of reading CSV files implemented by php, and analyzes the reading of CSV files and strings by php in the form of examples. For operating skills such as array traversal and conversion, friends in need can refer to

function read_csv($cvs) {
  $shuang = false;
  $str = file_get_contents($cvs);
  for ($i=0;$i<strlen($str);$i++) {
    if($str{$i}==&#39;"&#39;) {
      if($shuang) {
        if($str{$i+1}==&#39;"&#39;) {
          $str{$i} = &#39;*&#39;;
          $str{$i+1} = &#39;*&#39;;
        } else {
          $shuang = false;
        }
      } else {
        $shuang = true;
      }
    }
    if($str{$i}==&#39;,&#39;) {
      if($shuang) {
      } else {
        $str{$i} = &#39;|&#39;;
      }
    }
    if($str{$i}=="\n") {
      if($shuang) {
        $str{$i} = &#39;^&#39;;
      } else {
      }
    }
  }
  $str = str_replace(array(&#39;"&#39;,&#39;*&#39;),array(&#39;&#39;,&#39;"&#39;),$str);
  $a1 = explode("\n",$str);
  $array = array();
  foreach($a1 as $k=>$value) {
    if($value) {
      $value = str_replace("^","\n",$value);
      $array[$k] = explode("|",$value);
    }
  }
  return $array;
}

The above is the detailed content of php read CSV file example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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