note 107620 modified in function.str-getcsv by danbrown

From: Date: Thu, 10 May 2012 20:32:54 +0000
Subject: note 107620 modified in function.str-getcsv by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187814@lists.php.net to get a copy of this message
[A previous version of this function which I wrote] did not parse empty cells at the end of a line correctly. Here&#39;s the fixed version. Thanks to Graham Lawrence for the fix.&#13;&#10;&#60;?php&#13;&#10; function parse_csv($file,$comma=&#39;,&#39;,$quote=&#39;&#34;&#39;,$newline=&#34;\\n&#34;)&#13;&#10; {&#13;&#10; $db_quote = $quote . $quote;&#13;&#10; &#13;&#10; // Clean up file&#13;&#10; $file = trim($file);&#13;&#10; $file = str_replace(&#34;\\r\\n&#34;,$newline,$file);&#13;&#10; &#13;&#10; $file = str_replace($db_quote,&#39;&#34;&#39;,$file); // replace double quotes with &#34; HTML entities&#13;&#10; $file = str_replace(&#39;,&#34;,&#39;,&#39;,,&#39;,$file); // handle ,&#34;&#34;, empty cells correctly&#13;&#10;&#9;&#9;&#9;&#9;&#9;&#9;$file = str_replace(&#34;,&#34;\\n&#34;,&#34;,\\n&#34;,$file); // handle ,&#34;&#34;\\n empty cells correctly at the end of lines&#13;&#10; &#13;&#10; $file .= $comma; // Put a comma on the end, so we parse last cell &#13;&#10; &#13;&#10; &#13;&#10; $inquotes = false;&#13;&#10; $start_point = 0;&#13;&#10; $row = 0;&#13;&#10; $cellNo = 0;&#13;&#10; &#13;&#10; for($i=0; $i&#60;strlen($file); $i++) {&#13;&#10; &#13;&#10; $char = $file[$i];&#13;&#10; if ($char == $quote) {&#13;&#10; if ($inquotes) {&#13;&#10; $inquotes = false;&#13;&#10; }&#13;&#10; else { &#13;&#10; $inquotes = true;&#13;&#10; }&#13;&#10; } &#13;&#10; &#13;&#10; if (($char == $comma or $char == $newline) and !$inquotes) {&#13;&#10; $cell = substr($file,$start_point,$i-$start_point);&#13;&#10; $cell = str_replace($quote,&#39;&#39;,$cell); // Remove delimiter quotes&#13;&#10; $cell = str_replace(&#39;&#34;&#39;,$quote,$cell); // Add in data quotes&#13;&#10; if ($row&#62;0) $data[$row][$data[0][$cellNo]] = $cell;&#13;&#10; else $data[$row][] = $cell;&#13;&#10; $cellNo++;&#13;&#10; $start_point = $i + 1;&#13;&#10; if ($char == $newline) {&#13;&#10; $cellNo = 0;&#13;&#10; $row ++;&#13;&#10; }&#13;&#10; }&#13;&#10; }&#13;&#10; return $data;&#13;&#10; }&#13;&#10;?&#62; --was-- [A previous version of this function which I wrote] did not parse empty cells at the end of a line correctly. Here's the fixed version. Thanks to Graham Lawrence for the fix. <?php function parse_csv($file,$comma=',',$quote='"',$newline="\\n") { $db_quote = $quote . $quote; // Clean up file $file = trim($file); $file = str_replace("\\r\\n",$newline,$file); $file = str_replace($db_quote,'"',$file); // replace double quotes with " HTML entities $file = str_replace(',",',',,',$file); // handle ,"", empty cells correctly $file = str_replace(","\\n",",\\n",$file); // handle ,""\\n empty cells correctly at the end of lines $file .= $comma; // Put a comma on the end, so we parse last cell $inquotes = false; $start_point = 0; $row = 0; $cellNo = 0; for($i=0; $i<strlen($file); $i++) { $char = $file[$i]; if ($char == $quote) { if ($inquotes) { $inquotes = false; } else { $inquotes = true; } } if (($char == $comma or $char == $newline) and !$inquotes) { $cell = substr($file,$start_point,$i-$start_point); $cell = str_replace($quote,'',$cell); // Remove delimiter quotes $cell = str_replace('"',$quote,$cell); // Add in data quotes if ($row>0) $data[$row][$data[0][$cellNo]] = $cell; else $data[$row][] = $cell; $cellNo++; $start_point = $i + 1; if ($char == $newline) { $cellNo = 0; $row ++; } } } return $data; } ?> http://php.net/manual/en/function.str-getcsv.php

« previous php.notes (#187814) next »