Don't like ads? PRO users don't see any ads ;-)
Guest

PHP CSV Parser

By: a guest on Jun 27th, 2012  |  syntax: PHP  |  size: 2.22 KB  |  hits: 44  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. /*
  4. * Custom CSV File Parser class v 2.0 by ajc20
  5. *
  6. * encode decodes csv strings and csv files
  7. *
  8. * Support functions:
  9. *
  10. * STRING csv_encode($array, $delimiter=',')
  11. * => encodes a given array to a csv string format
  12. *
  13. * ARRAY csv_decode($string, $delimiter=',')
  14. * => decodes the csv string format to an array
  15. *
  16. * BOOLEAN csv_encode_file($array, $file, $delimiter=',')
  17. * => encodes the given array to csv string format
  18. *    and write it to the file
  19. *
  20. * ARRAY csv_decode_file($file, $delimiter=',')
  21. * => decodes the csv file to an array data
  22. *
  23. */
  24. class libCSV{
  25.         public function csv_encode($array, $delimiter=','){
  26.                 $csv_string = null;
  27.                 if(is_array($array)){
  28.                         $length = count($array);
  29.                         for($k = 0; $k < $length; $k++){
  30.                                 $array[$k] = implode($delimiter, $array[$k]);
  31.                         }
  32.                         $csv_string = implode(PHP_EOL, $array);
  33.                 } else die("The given <b><u>array</u></b> is not an array.");
  34.                 return $csv_string;
  35.         }
  36.        
  37.         public function csv_decode($string, $delimiter=','){
  38.                 $csv_array = null;
  39.                 if(is_string($string)){
  40.                        
  41.                         if($string == "" || $string == null){
  42.                                 return NULL;
  43.                         }
  44.                        
  45.                         $string = preg_replace('~\r[\n]?~', "_", $string);
  46.                         $rows = explode("_", $string);
  47.                         $cols = Array();
  48.                        
  49.                         $c = 0;
  50.                         foreach($rows as $columns){
  51.                                 $cols[$c] = explode($delimiter, $columns);
  52.                                 $c++;
  53.                         }
  54.                         $csv_array = $cols;
  55.                 } else die("The given <b><u>string</u></b> is not a string.");
  56.                 return $csv_array;
  57.         }
  58.        
  59.         public function csv_encode_file($array, $filename, $delimiter=','){
  60.                 $encoded = false;
  61.                 if(is_array($array)){
  62.                         if(file_exists($filename)){
  63.                                 $data = $this->csv_encode($array, $delimiter);
  64.                                 $fileh = fopen($filename, 'w');
  65.                                 $write = fwrite($fileh, $data);
  66.                                 fclose($fileh);
  67.                                
  68.                                 if($write !== false);
  69.                                         $encoded = true;
  70.                         } else die("The File ".$filename." does not exists.");
  71.                 } else die("The given <b><u>array</u></b> is not an array.");
  72.                
  73.                 return $encoded;
  74.         }
  75.        
  76.         public function csv_decode_file($filename, $delimiter=','){
  77.                 $data = null;
  78.                 if(file_exists($filename)){
  79.                         $data = file_get_contents($filename);
  80.                         $data = $this->csv_decode($data, $delimiter);
  81.                 } else die("The File ".$filename." does not exists");
  82.                
  83.                 return $data;
  84.         }
  85. }
  86.  
  87. ?>