Advertisement
HwapX

Read encoded CSV

Feb 9th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4.  
  5. header('content-type: text/plain;charset=utf8');
  6.  
  7.  
  8. function readCSV($path, $hasHeader = true, $encoding = "UTF-16") {
  9.     $contents = file_get_contents($path);
  10.     $contents = iconv($encoding, "UTF-8", $contents);
  11.     $lines    = explode("\r\n", $contents);
  12.     $lines    = array_filter($lines, 'trim');
  13.     $rows     = array_map('str_getcsv', $lines);
  14.    
  15.     if($hasHeader) {
  16.         $headers  = array_shift($rows);
  17.        
  18.         foreach($rows as &$row) {
  19.             $data     = [];
  20.        
  21.             foreach($row as $idx => $value) {
  22.                 $data[$headers[$idx]] = $value;
  23.             }
  24.            
  25.             $row = $data;
  26.         }
  27.        
  28.     }
  29.    
  30.     return $rows;
  31. }
  32.  
  33.  
  34. $csv = readCSV('google.csv');
  35.  
  36. print_r($csv);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement