note 56192 added to function.fgetcsv

From: Date: Fri, 26 Aug 2005 14:16:24 +0000
Subject: note 56192 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-94223@lists.php.net to get a copy of this message
Some of you might need this: CSV-Parser optimized for -speed- which can handle linebreaks in quoted strings This parser can not handle unquoted data containing quotes ("), i.e. apple, ban"ana, cherry <?php // csv.inc.php // // Usage: // use argument chopnl=true to autoconvert linebreaks in strings to spaces $csv_data = "apple; banana; \"multi-\nline\";\"string with \"\" quoted quotes\";\n"; $csv_data .= "second;line;with;trailing;colon\n"; $csv_data .= "third;line;without;trailing;colon;"; $prepared = csv_prepare($csv_data); while ($myrow = csv_getrow(&$prepared, false)) { echo "Row ".csv_rownumber(&$prepared).": "; for ($i = 0;$i < $myrow["c"]; $i++) echo "[".htmlspecialchars($myrow[$i])."]"; echo "<BR>\n"; } // ******** public functions ******** function csv_prepare($data) { $rc = array(); $rc[0] = strtr($data,array("\r\n" => "\n")); $rc[0] = strtr($rc[0],array("\r" => "\n")); $rc[1] = 0; $rc[2] = strlen($rc[0]); $rc[3] = 0; return $rc; } // data[0] = csv-data // data[1] = startindex // data[2] = total length csv // data[3] = currentrow starting from 1 (only for informational purposes) function csv_rownumber(&$data) { return $data[3]; } function csv_getrow(&$data, $chopnl) { $data[3] += 1; $rc = array(); $line = csv__getline(&$data); $line = trim($line); $len = strlen($line); $start = 0; if ($len == 0 && $data[1] >= $data[2]) { return false; } $iter = 0; while ($iter < 500) { $item = csv__getitem(&$line, $chopnl, &$start, $len); $rc[$iter] = $item; $iter += 1; if ($start >= $len) break; } $rc["c"] = $iter; return $rc; } // ************ Internals ********* function csv__getline(&$data) { $sep = csv__extract(&$data[0], "\n", $data[1], $data[2]); $oi = $data[1]; $data[1] = $sep + 1; return substr($data[0],$oi,$sep - $oi); } function csv__getitem(&$data, $chopnl, &$start, $len) { $sep = csv__extract(&$data, ";",$start, $len); $rc = trim(substr($data,$start,$sep - $start)); $start = $sep + 1; if (substr($rc,0,1) == "\"") { if (substr($rc,strlen($rc) - 1,1) == "\"") { $rc = substr($rc,1,strlen($rc) - 2); } } $rc = strtr($rc,array("\"\"" => "\"")); if ($chopnl) { $rc = strtr($rc,array("\n" => " ")); } return trim($rc); } function csv__extract(&$data, $sep, $startidx, $len) { $instr = false; for ($i=$startidx;$i < $len; $i+=1) { $c = $data[$i]; if ($c == "\"") { if ($instr) { $c2 = ($i < $len - 1) ? $data[$i + 1] : ""; if ($c2 == "\"") { $i += 1; continue; } else { $instr = false; } } else { $instr = true; } } elseif ($c == $sep) { if ($instr) { continue; } else { return $i; } } } return $len; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56192 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56192&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56192&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56192&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56192&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56192&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56192&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56192&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94223) next »