Method of reading CSV file function implemented in PHP

墨辰丷
Release: 2023-03-28 07:34:01
Original
1499 people have browsed it

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!