Advertisement
Guest User

PHP CSV Parser

a guest
Jul 30th, 2012
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.33 KB | None | 0 0
  1. <?php
  2. /**
  3.  * CSV_parser
  4.  *
  5.  * @package  
  6.  * @author Dave's Simple Project
  7.  * @copyright MESMERiZE
  8.  * @version 2012
  9.  * @access public
  10.  */
  11. class CSV_parser
  12. {
  13.     private $source;
  14.     private $array;
  15.     private $length;
  16.     private $delimiter;
  17.     private $class;
  18.     private $padding;
  19.     private $border;
  20.     private $id;
  21.     private $width;
  22.  
  23.     /**
  24.      * CSV_parser::__construct()
  25.      *
  26.      * @param mixed $source
  27.      * @param integer $length
  28.      * @param string $delimiter
  29.      * @return
  30.      */
  31.     public function __construct($source, $length = 8000, $delimiter = ',')
  32.     {
  33.         try
  34.         {
  35.             // Lets try to open the file and check if its readable else throw an error.
  36.             if (!isset($source) || !is_readable($source))
  37.             {
  38.                 throw new Exception('File Not Found!');
  39.                 return false;
  40.             } else
  41.             {
  42.                 //set the source file
  43.                 $this->source = $source;
  44.                 $this->length = $length;
  45.                 $this->delimiter = $delimiter;
  46.                 return true;
  47.             }
  48.         }
  49.         catch (exception $e)
  50.         {
  51.             // Send an error message :)
  52.             echo $e->getMessage();
  53.         }
  54.         parent::__construct();
  55.     }
  56.     /**
  57.      * CSV_parser::toArray()
  58.      *
  59.      * @return
  60.      */
  61.     public function toArray()
  62.     {
  63.         // 1. First open the source file
  64.         $handler = fopen($this->source, 'r');
  65.         // 2. Turn the CSV to array
  66.         while (($data = fgetcsv($handler, $this->length, $this->delimiter)) !== false)
  67.         {
  68.             $a[] = $data;
  69.         }
  70.         // 3. Get the first index to be used as the key
  71.         $h = $a[0];
  72.         // 4. Lets remove the $h variable's empty values
  73.         foreach ($h as $k => $v)
  74.         {
  75.             if ($v != '')
  76.             {
  77.                 $headers[$k] = $v;
  78.             }
  79.         }
  80.         // 4. Remove the first index and leave the others to be used as the value
  81.         $a = array_slice($a, 1);
  82.         // 5. Make an empty array
  83.         $array = array();
  84.         // 6. Lets loop the values
  85.         foreach ($a as $k => $v)
  86.         {
  87.             $i = 0;
  88.             // then loop the headers then for each headers lets get the values
  89.             // from variable $a based on how many the headers are. So we increment.
  90.             foreach ($headers as $key => $value)
  91.             {
  92.                 $array[$k][$value] = $v[$i];
  93.                 $i++;
  94.             }
  95.         }
  96.         return $array;
  97.         fclose($handler);
  98.     }
  99.     /**
  100.      * CSV_parser::toTable()
  101.      *
  102.      * @param string $width
  103.      * @param integer $border
  104.      * @param integer $spacing
  105.      * @param integer $padding
  106.      * @param string $class
  107.      * @param mixed $id
  108.      * @return
  109.      */
  110.     public function toTable($width = '100%', $border = 1, $spacing = 0, $padding = 5,
  111.         $class = 'mytable', $id = null)
  112.     {
  113.         $this->width = $width;
  114.         $this->class = $class;
  115.         $this->spacing = $spacing;
  116.         $this->padding = $padding;
  117.         $this->border = $border;
  118.         $this->id = $id;
  119.  
  120.         $table = '<table width="' . $this->width . '" class="' . $this->class .
  121.             '" cellspacing="' . $this->spacing . '" cellpadding="' . $this->padding .
  122.             '" id="' . $this->id . '" border="' . $this->border . '">';
  123.         // 1. Lets create some table headers
  124.         $table .= '<thead><tr>';
  125.         foreach ($this->toArray() as $key => $value)
  126.         {
  127.             $headers = $value;
  128.         }
  129.         $headers = array_keys($headers);
  130.         foreach ($headers as $th)
  131.         {
  132.             $table .= '<th>' . $th . '</th>';
  133.         }
  134.         $table .= '</tr></thead>';
  135.         // Lets create the table body
  136.         $table .= '<tbody>';
  137.         foreach ($this->toArray() as $key => $value)
  138.         {
  139.             $table .= '<tr>';
  140.             foreach ($value as $val)
  141.             {
  142.                 $table .= '<td>' . $val . '</td>';
  143.             }
  144.             $table .= '<tr>';
  145.  
  146.         }
  147.         $table .= '</tbody>';
  148.         $table .= '</table>';
  149.         return $table;
  150.  
  151.     }
  152.     /**
  153.      * CSV_parser::toJSON()
  154.      *
  155.      * @return
  156.      */
  157.     public function toJSON()
  158.     {
  159.         return json_encode($this->toArray());
  160.     }
  161.  
  162. }
  163. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement