"; $c = new csv(); $c->openFile($csvFile); $c->_parseHeader(); $c->_parseRows(); print_r($c->header); print_r($c->rows); echo ""; class csv{ var $header; var $rows; var $_rows; var $_myFile; var $_separator; var $_textDelimeter; function csv(){ //constructor } function openFile($file,$separator=',',$textDelimeter='"'){ // will be replaced for a proper fopen later $this->_myFile = $file; $this->_separator=$separator; $this->_textDelimeter=$textDelimeter; $tmpLines=explode("\n",$this->_myFile); $counter=0; $this->_rows=array(); foreach ($tmpLines as $line) { if ($counter<1){ $this->header=$line; $counter++; }else{ $this->_rows[]=$line; } } unset($counter); unset($tmpLines); unset($line); } function _parseHeader($line=""){ // first we split the line with the separator if ($line==""){ $line=$this->header; } $this->header = $this->_csvExplode($line); unset($line); } function _parseRows(){ $this->rows=array(); foreach ($this->_rows as $row){ $this->rows[]=$this->_csvExplode($row); } } function _csvExplode($line){ // run through each character... if we are inside a "' we dont explode else, explode $open = false; $exploded=array(); $currentCol=""; for ($i=0;$i_textDelimeter){ $open=($open?false:true); } if ($c==$this->_separator){ if ($open){ $currentCol.=$c; }else{ $exploded[]=ltrim(rtrim($currentCol,$this->_textDelimeter),$this->_textDelimeter); $currentCol=""; } } if ($c!=$this->_separator){ $currentCol.=$c; } if ($i==(strlen($line)-1)){ $exploded[]=ltrim(rtrim($currentCol,$this->_textDelimeter),$this->_textDelimeter); $currentCol=""; } } return $exploded; } } ?>