note 45008 added to function.fgetcsv

From: Date: Mon, 23 Aug 2004 10:26:19 +0000
Subject: note 45008 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-75153@lists.php.net to get a copy of this message
Hi All, I modified Miked CVS class described before so that in the constructor of the CSV class, i give one more parameter to give the definition of the rows to put in the 2D array instead of counting on the first line of the file to define it for me. Any comment is welcome function CSV($filename,$delim,$linedef,$enclosed_by="",$eol="\n") { //open the file $this->raw_data = implode("",file($filename)); // make sure all CRLF's are consistent $this->CRLFclean(); // use custom $eol (if exists) if($eol!="\n" AND trim($eol)=="") $this->error("Couldn't split data via empty \$eol, please specify a valid end of line character."); else { $this->new_data = @explode($eol,$this->raw_data); if(count($this->new_data)==0) $this->error("Couldn't split data via given \$eol.<li>\$eol='".$eol."'"); } // create data keys with the line definition given in params $temp = @explode($delim,$linedef); foreach($temp AS $field_index=>$field_value) { $this->mapping[] = $this->validKEY($field_value); } // fill the 2D array using the keys given foreach($this->new_data AS $index1=>$line) { $temp = @explode($delim,$line); if(trim($line)=="") // skip empty lines continue; elseif(count($temp)==0) // line didn't split properly so record error $this->errors[] = "Couldn't split data line ".$c." via given \$delim."; else { $data_set = array(); foreach($temp AS $field_index=>$field_value) { // Remove enclose characters $this->stripENCLOSED($field_value,$enclosed_by); $data_set[$this->mapping[$field_index]] = $field_value; } if(count($data_set)>0) $this->results[] = $data_set; } unset($data_set); } return $this->results; } ---- Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+45008 Delete -- http://master.php.net/manage/user-notes.php?action=delete+45008&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+45008&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#75153) next »