Example #1
0
 /**
  * This function will load a CSV file.
  *
  * @access public
  * @static
  * @param array $config                             the configuration array
  * @return CSV                                      an instance of the CSV class containing
  *                                                  the contents of the file.
  *
  * @see http://www.php.net/manual/en/function.fgetcsv.php
  */
 public static function load($config = array())
 {
     $csv = new CSV($config);
     if (file_exists($csv->file_name)) {
         if (($fp = fopen($csv->file_name, 'r')) !== FALSE) {
             $eol = $csv->eol == "\r\n" ? array(13, 10) : array(ord($csv->eol));
             // 13 => cr, 10 => lf
             $buffer = '';
             while (($char = fgetc($fp)) !== FALSE) {
                 // load char by char, to replace line endings
                 if (in_array(ord($char), $eol)) {
                     $buffer .= "\r\n";
                 } else {
                     $buffer .= $char;
                 }
             }
             fclose($fp);
             $rows = explode("\r\n", $buffer);
             $enclosure = $csv->enclosure;
             $delimiter = $enclosure . $csv->delimiter . $enclosure;
             if (empty($enclosure)) {
                 $enclosure = " \t\n\r\v";
             }
             $regex = '/' . $delimiter . '/';
             foreach ($rows as $row) {
                 $row = trim($row, $enclosure);
                 //$columns = explode($delimiter, $row);
                 $columns = preg_split($regex, $row);
                 $csv->add_row($columns);
             }
         }
     }
     return $csv;
 }
Example #2
0
 /**
  * This function will create an instance of the CSV class using the data contained
  * in the result set.
  *
  * @access public
  * @param array $config                             the configuration array
  * @return CSV                                      an instance of the CSV class
  */
 public function as_csv(array $config = array())
 {
     $csv = new CSV($config);
     if ($this->is_loaded()) {
         switch ($this->type) {
             case 'array':
             case 'object':
                 foreach ($this->records as $record) {
                     $csv->add_row((array) $record);
                 }
                 break;
             default:
                 if (class_exists($this->type)) {
                     if ($this->records[0] instanceof DB_ORM_Model or method_exists($this->records[0], 'as_array')) {
                         foreach ($this->records as $record) {
                             $csv->add_row($record->as_array());
                         }
                     } else {
                         if ($this->records[0] instanceof Iterator) {
                             foreach ($this->records as $record) {
                                 $row = array();
                                 foreach ($record as $column) {
                                     $row[] = $column;
                                 }
                                 $csv->add_row($row);
                             }
                         } else {
                             foreach ($this->records as $record) {
                                 $csv->add_row(get_object_vars($record));
                             }
                         }
                     }
                 }
                 break;
         }
     }
     return $csv;
 }