Home  >  Article  >  Backend Development  >  Open CSV file with PHP

Open CSV file with PHP

WBOY
WBOYOriginal
2016-07-25 09:02:12981browse
Open CSV file with PHP
  1. //Set UTF-8 encoding
  2. setlocale(LC_ALL, 'en_US.UTF-8');
  3. //csv path
  4. $csv_file = "csv/excel.csv";
  5. //Open in read-only mode File
  6. $handle = fopen($csv_file, "r");
  7. //Define the result array
  8. $listarr = array();
  9. //Open a row in csv and split it into an array with ","
  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. }
  15. $listarr[] = $data;
  16. }
  17. return $listarr;
Copy code


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