note 83522 added to function.fgetcsv

From: Date: Fri, 30 May 2008 02:33:22 +0000
Subject: note 83522 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-139989@lists.php.net to get a copy of this message
Hey everybody... I know that a lot of people have written code to parse CSVs etc... I have written one which I believe is a lot smaller and faster than those listed here... <?php /* class.parser.php Dynamically parses a CSV file and returns the values as array for processing. */ class parser { function doParse($csvFile,$sep) { $csvFile = file($csvFile); foreach ($csvFile as $key=>$value) { $v = explode($sep,$value); foreach ($v as $kk=>$lineItem) { $csv[$key][$kk] = trim(trim($lineItem),"\""); } } return $csv; // an associative array of the csv. } function dumpCSV($data,$sep,$heading = "") { /* build the "csv" from a 2 dimensional array $result = $parser->dumpCSV($data,",",$heading); where $data is an array formed similar to doParse (see above), and $heading is the heading line for the CSV (titles etc).... Something like $heading = "\"Booking Number\",\"Booking Date\"\n"; if you do not want/need a headling line, do not include $heading */ unset($message); $message[] = $heading; $x = 0; foreach ($data as $key=>$v) { unset($tempmsg); foreach ($v as $item) { $tempmsg .= "\"".$item."\"".$sep; } $message[$x] = trim($tempmsg,","); // gets rid of excess , @ the end of each line. $x++; } //print_r($message); // calculate accurate file size for the "downloaded file" foreach ($message as $line) { $bytes .= strlen($line); } return $message; } function downloadCSV($data,$filename) { header ("Content-Type: application/vnd.ms-excel"); header ("Content-disposition: attachment; filename=\"".$filename.".csv\""); header ("Content-length: $bytes"); foreach ($data as $line) { echo $line."\n"; } } function searchCSVKey($data,$searchkey) { foreach ($data as $key=>$v) { foreach ($v as $item) { if ($item == $searchkey) { $returnvalue = $data[$key]; $returnvalue['line'] = $key; break 2; } } } if ($returnvalue == "") { $returnvalue['0'] = "NULL"; $returnvalue['line'] = 0; } return $returnvalue; } function getPartialCSVAlpha($data,$start) { // gets from $start to the end of the CSV. useful for searching for ($x = $start + 1; $x <= count($data); $x++) { $d[] = $data[$x]; } return $d; } function getPartialCSVOmega($data,$finish) { // 1 to the $finish of the CSV and return for ($x = 0; $x < $finish; $x++) { $d[] = $data[$x]; } return $d; } function getSection($data,$startSearch,$finishSearch) { $r1 = $this->searchCSVKey($data,$startSearch); $data = $this->getPartialCSVAlpha($data,$r1['line']); // gets rid of the first section not needed. $r2 = $this->searchCSVKey($data,$finishSearch); $data = $this->getPartialCSVOmega($data,$r2['line']); // sections down the CSV. return $data; } function getSectionByLine($data,$start,$finish) { for ($x = $start; $x <= $finish; $x++) { $output[] = $data[$x]; } return $output; } function addValues3($array1,$array2) { foreach ($array1 as $key1=>$val1) { foreach ($val1 as $i=>$v) { if ((is_numeric($array1[$key1][$i]) == TRUE) && ($i > 1)) { $output[$key1][$i] = floatval($array2[$key1][$i]) + floatval($array1[$key1][$i]); } else { $output[$key1][$i] = $array1[$key1][$i]; } } } return $output; } } // </eof> // ?> Hope it helps all :) ---- Server IP: 69.147.83.197 Probable Submitter: 58.6.125.196 ---- Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php Edit -- https://master.php.net/note/edit/83522 Del: integrated -- https://master.php.net/note/delete/83522/integrated Del: useless -- https://master.php.net/note/delete/83522/useless Del: bad code -- https://master.php.net/note/delete/83522/bad+code Del: spam -- https://master.php.net/note/delete/83522/spam Del: non-english -- https://master.php.net/note/delete/83522/non-english Del: in docs -- https://master.php.net/note/delete/83522/in+docs Del: other reasons-- https://master.php.net/note/delete/83522 Reject -- https://master.php.net/note/reject/83522 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#139989) next »