Home > Article > Backend Development > Method of reading CSV file function implemented in PHP
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}=='"') { if($shuang) { if($str{$i+1}=='"') { $str{$i} = '*'; $str{$i+1} = '*'; } else { $shuang = false; } } else { $shuang = true; } } if($str{$i}==',') { if($shuang) { } else { $str{$i} = '|'; } } if($str{$i}=="\n") { if($shuang) { $str{$i} = '^'; } else { } } } $str = str_replace(array('"','*'),array('','"'),$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!