Home  >  Article  >  Backend Development  >  A small example of php reading (opening) csv files

A small example of php reading (opening) csv files

WBOY
WBOYOriginal
2016-07-25 08:55:041160browse
  1. //Set UTF-8 encoding

  2. setlocale(LC_ALL, 'en_US.UTF-8');
  3. //csv path
  4. $csv_file = "csv/excel. csv";
  5. //Open the file in read-only mode
  6. $handle = fopen($csv_file, "r");
  7. //Define the result array
  8. $listarr = array();
  9. //Open a line in csv and use " ,"Split into arrays
  10. while($data = fgetcsv($handle, 1000, ",")){
  11. $num = count($data);
  12. for($i=0; $i<$num; $i++ ){
  13. $data[$i] = mb_convert_encoding($data[$i],"UTF-8","UTF-8");
  14. }// bbs.it-home.org
  15. $listarr[] = $ data;
  16. }

  17. return $listarr;

Copy code
>>> More about generating (exporting) csv with php.


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