note 45008 deleted from function.fgetcsv by aidan
| From: | aidan@php.net | Date: | Wed, 08 Sep 2004 05:41:34 +0000 |
| Subject: | note 45008 deleted from function.fgetcsv by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76194@lists.php.net to get a copy of this message | ||
Note Submitter:
----
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;
}