Skip to content

Instantly share code, notes, and snippets.

@dboskovic
Created July 2, 2013 21:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dboskovic/5913307 to your computer and use it in GitHub Desktop.
<?php
namespace Bundles\CSV;
ini_set('auto_detect_line_endings', 1);
use Exception;
use e;
/**
* CSV File Parser
*
* @author David D. Boskovic
*/
class csv {
/**
* Array that the parsed file is stored in.
*/
public $matrix = array();
public $assoc;
public $exists;
public $file;
public function __construct($file = false) {
if(!$file) return true;
if(file_exists($file))
$this->load($file);
else
$this->load($file, 1);
}
/**
* Parse a string or file into the matrix.
* @author David Boskovic
* @param $src [string], $string [bool]
* @since 07/10/2012
*/
public function load($src = false, $string = false) {
/**
* Has a file?
*/
$this->file = $string ? false : $src;
/**
* If we're using a file, load it, otherwise parse the string.
*/
if($this->file && ($handle = @fopen($file, "r")) !== false) {
/**
* Iterate through the file lines and add the items.
*/
$i = 0;
while (($data = fgetcsv($handle, 4096, ",")) !== FALSE) {
$this->add($data, $i ? false : true);
$i++;
}
fclose($handle);
}
else {
/**
* Iterate through the string lines and add the items.
*/
$data = explode($src, "\n");
foreach($data as $key => $line) {
$fields = str_getcsv($line);
$this->add($fields, $key ? false : true);
}
}
}
// Return as matrix
public function matrix() {
return $this->matrix;
}
// Return as associative array
public function assoc() {
$array = array();
foreach ($this->matrix as $k => $v) {
// procss each row
// $k = row number
// $v = array of columns
if ($k == 0) {}else{ // working on row 2 or higher
$array[$k - 1] = array('index' => $k - 1);
foreach ($this->matrix[0] as $k1=>$v1) {
// store the column header as the key.
// Store the value associated with this item as the value.
$array[$k - 1][strtolower($v1)] = $v[$k1];
}
}
}
$this->assoc = $array;
return $this->assoc;
}
public function headers() {
return $this->matrix[0];
}
public function add($line, $multiD = false) {
if($multiD) {
$this->matrix[] = $this->_flatten(false,$line);
}
else
$this->matrix[] = $line;
return true;
}
private function _flatten($field = false, $array) {
$out = array();
foreach($array as $key => $value) {
$fkey = $field ? "$field/$key" : $key;
if(is_array($value))
$out = array_merge($out, $this->_flatten($fkey, $value));
else
$out[$fkey] = $value;
}
return $out;
}
public function output($filename = 'export') {
if(!is_array($this->matrix[0])) return false;
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename.csv\";" );
header("Content-Transfer-Encoding: binary");
$lines = array();
/**
* Set the headers
*/
$keys = $this->_find_all_keys();
$seed = array_flip($keys);
foreach($seed as &$titem) {
$titem = '';
}
$lines[] = $this->_line_to_csv($keys);
/**
* Iterate through the lines and create the csv file.
*/
foreach($this->matrix as $line) {
$lines[] = $this->_line_to_csv($line, $seed);
}
/**
* Print out the string.
*/
$output = implode("\n", $lines);
print $output;
}
private function _find_all_keys() {
$keys = array();
foreach($this->matrix as $item) {
$keys =array_merge($keys, $item);
}
$keys = array_keys($keys);
foreach($keys as &$key) {
if(is_numeric($key))
unset($key);
}
return $keys;
}
private function _line_to_csv($line, $seed = false) {
$return = $seed ? $seed : array();
foreach($line as $key => $field) {
if(is_numeric($field))
$return[$key] = $field;
else {
$field = str_replace(array("\r\n", "\n", "\r"),'\n',$field);
$return[$key] = '"'.addslashes($field).'"';
}
}
return implode(',', $return);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment