note 83407 added to function.fgetcsv
| From: | Verlustmeldung at gmx dot de | Date: | Sat, 24 May 2008 07:59:14 +0000 |
| Subject: | note 83407 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139687@lists.php.net to get a copy of this message | ||
i wrote a decent litte class which is able to import excel 2003 .csv files kickass fast and easy.
havent tested it with other excel versions, but they should work as well.
[code]
class read_csv {
function read_csv () {
// nothing
}
function read_csv_run($f="") {
if ( $f AND is_file($f) ) {
// set excel type delimiter, etc
$delimiter = ';';
$enclosure = '"';
// read file & parse
$input = file($f);
$csv = array();
foreach ( $input as $key => $value ) {
// rtrim crap at the end of the string
$tmp = explode($delimiter,rtrim($value));
// parse
$in_quote = false;
$arr = array();
foreach ( $tmp as $key => $value ) {
if ( $in_quote ) {
if ( $this->read_csv_has_quote($value,$enclosure) ) {
$in_quote = false;
$value = substr_replace($value,'',-1,1);
}
$key = (count($arr)-1);
$arr[$key] .= $delimiter.$value; // continue last array element
} else {
if ( $this->read_csv_has_quote($value,$enclosure) ) {
$in_quote = true;
$value = substr_replace($value,'',0,1);
} else if ( substr($value,0,1) == $enclosure AND substr($value,-1,1) == $enclosure ) {
// string is quoted, remove quotes
$value = substr_replace($value,'',0,1); // start
$value = substr_replace($value,'',-1,1); // end
}
$arr[] = $value; // append to array
}
}
foreach ( $arr as $key => $value ) {
$arr[$key] = str_replace($enclosure.$enclosure,$enclosure,$value);
}
// append to array
$csv[] = $arr;
} // end foreach
echo nl2br(print_r($csv,1));
} // end if
} // end func
function read_csv_has_quote ($str="",$enc="") {
$c = substr_count($str,$enc);
if ( stristr(($c/2),".") ) {
return true;
}
}
} // end class
$csv =& new read_csv();
$csv->read_csv_run("katalog_excel_D.csv");
[/code]
mfg RR
----
Server IP: 69.147.83.197
Probable Submitter: 87.182.217.9
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- https://master.php.net/note/edit/83407
Del: integrated -- https://master.php.net/note/delete/83407/integrated
Del: useless -- https://master.php.net/note/delete/83407/useless
Del: bad code -- https://master.php.net/note/delete/83407/bad+code
Del: spam -- https://master.php.net/note/delete/83407/spam
Del: non-english -- https://master.php.net/note/delete/83407/non-english
Del: in docs -- https://master.php.net/note/delete/83407/in+docs
Del: other reasons-- https://master.php.net/note/delete/83407
Reject -- https://master.php.net/note/reject/83407
Search -- https://master.php.net/manage/user-notes.php