just another csv file parse function
<?php
define('CSV_BOTH', 1);
define('CSV_ASSOC', 2);
define('CSV_NUM', 3);
function parse_csv($filename, $result_type = CSV_BOTH) {
if(!file_exists($filename)) {
die("file (" . $filename . ") does not
existn");
}
$lines = file($filename);
$title_line = trim(array_shift($lines));
$titles = split(",", $title_line);
$records = array();
foreach($lines as $line_num => $line) {
$subject = trim($line);
$fields = array();
for($field_num = 0; $field_num < count($titles);
$field_num++) {
if($subject == '"') {
preg_match('/^"(([^"]|\")*)",?(.*)$
/', $subject, $matches);
$value = $matches[1];
$subject = $matches[3];
if($result_type == CSV_BOTH || $result_type ==
CSV_ASSOC) {
$fields[$titles[$field_num]] = $value;
}
if($result_type == CSV_BOTH || $result_type == CSV_NUM)
{
$fields[$field_num] = $value;
}
} else {
preg_match('/^([^,]*),?(.*)$/', $subject, $matches);
$value = $matches[1];
$subject = $matches[2];
if($result_type == CSV_BOTH || $result_type ==
CSV_ASSOC) {
$fields[$titles[$field_num]] = $value;
}
if($result_type == CSV_BOTH || $result_type == CSV_NUM)
{
$fields[$field_num] = $value;
}
}
}
$records[] = $fields;
}
return $records;
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 69.48.114.91
----
Manual Page -- htt
p://www.php.net/manual/en/function.fgetcsv.php
Edit -- https://master
.php.net/note/edit/72749
Del: integrated -- h
ttps://master.php.net/note/delete/72749/integrated
Del: useless -- http
s://master.php.net/note/delete/72749/useless
Del: bad code -- htt
ps://master.php.net/note/delete/72749/bad+code
Del: spam -- https:/
/master.php.net/note/delete/72749/spam
Del: non-english --
https://master.php.net/note/delete/72749/non-english
Del: in docs -- http
s://master.php.net/note/delete/72749/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/72749
Reject -- https://mast
er.php.net/note/reject/72749
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|