Home  >  Article  >  Backend Development  >  Method of reading CSV file function implemented in PHP

Method of reading CSV file function implemented in PHP

墨辰丷
墨辰丷Original
2018-05-26 13:48:551499browse

This article mainly introduces the function of reading CSV files implemented by PHP, and analyzes the operation skills of PHP reading CSV files and strings, array traversal and conversion, etc. in the form of examples. Friends in need can refer to it

The details are as follows:

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 entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

How to use JS time-sharingfunctionfor performance optimization

php Commonly used systemsFunctionDetailed explanation

PHP classes and structuresFunction

The above is the detailed content of Method of reading CSV file function implemented in PHP. 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