note 72749 added to function.fgetcsv

From: Date: Wed, 31 Jan 2007 16:23:55 +0000
Subject: note 72749 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-122427@lists.php.net to get a copy of this message
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 exist\n"); } $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{0} == '"') { 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 -- http://www.php.net/manual/en/function.fgetcsv.php Edit -- https://master.php.net/note/edit/72749 Del: integrated -- https://master.php.net/note/delete/72749/integrated Del: useless -- https://master.php.net/note/delete/72749/useless Del: bad code -- https://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 -- https://master.php.net/note/delete/72749/in+docs Del: other reasons-- https://master.php.net/note/delete/72749 Reject -- https://master.php.net/note/reject/72749 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#122427) next »