reader = new NkmCSVReader(); } public function createTemplate($arr=array()) { $this->csvTemplate = $arr; }//end:function public function addEntry($arr=array()) { foreach($arr as $Index=>$Value) { $arr[$Index] = $this->csvCapsule.str_replace($this->csvCapsule,$this->csvCapsule.$this->csvCapsule,$Value).$this->csvCapsule; }//end:foreach $this->csvCollection[] = $arr; }//end:function public function buildDoc() { $docLine = ''; $csvTemplate = $this->csvTemplate; // Add Header foreach($csvTemplate as $Index=>$Title) { $csvTemplate[$Index] = $this->csvCapsule.str_replace($this->csvCapsule,$this->csvCapsule.$this->csvCapsule,$Title).$this->csvCapsule; }//end:foreach $docLine.=implode($this->csvDelimeter,$csvTemplate).$this->csvLine; // Add CSV Information foreach($this->csvCollection as $csvCollectionItem) { $collectionDeposit = array(); foreach($csvTemplate as $Index=>$Title) { $collectionDeposit[] = $csvCollectionItem[$Index]; }//end:foreach $docLine.=implode($this->csvDelimeter,$collectionDeposit).$this->csvLine; }//end:foreach return $docLine; }//end:function }//end:class /** * CSVReader Class * * $Id: Csv.php,v 1.4 2013/11/18 15:19:26 samuel Exp $ * * Allows to retrieve a CSV file content as a two dimensional array. * Optionally, the first text line may contains the column names to * be used to retrieve fields values (default). * * Let's consider the following CSV formatted data: * * "col1";"col2";"col3" * "11";"12";"13" * "21;"22;"2;3" * * It's returned as follow by the parsing operation with first line * used to name fields: * * Array( * [0] => Array( * [col1] => 11, * [col2] => 12, * [col3] => 13 * ) * [1] => Array( * [col1] => 21, * [col2] => 22, * [col3] => 2;3 * ) * ) * * @author Pierre-Jean Turpeau * @link http://www.codeigniter.com/wiki/CSVReader */ class NkmCSVReader { public $fields; /** columns names retrieved after parsing */ public $separator = ';'; /** separator used to explode each line */ public $enclosure = '"'; /** enclosure used to decorate each field */ public $max_row_size = 20000; /** maximum row size to be used for decoding */ /** * Parse a file containing CSV formatted data. * * @access public * @param string * @param boolean * @return array */ public function parse_file($p_Filepath, $p_NamedFields = true, $skip_first_line = false) { $content = false; $file = fopen($p_Filepath, 'r'); if($p_NamedFields) { $this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure); } $cpt = 0; while( ($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) { $cpt ++; if ($skip_first_line && $cpt == 1) { continue; } if( $row[0] != null ) { // skip empty lines if( !$content ) { $content = array(); } if( $p_NamedFields ) { $items = array(); // I prefer to fill the array with values of defined fields foreach( $this->fields as $id => $field ) { if( isset($row[$id]) ) { $items[$field] = $row[$id]; } } $content[] = $items; } else { $content[] = $row; } } } fclose($file); return $content; } } ?>