note 107620 modified in function.str-getcsv by tularis

From: Date: Wed, 22 Feb 2012 21:04:33 +0000
Subject: note 107620 modified in function.str-getcsv by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-186296@lists.php.net to get a copy of this message
The previous function did not parse empty cells at the end of a line correctly. Here\&#039;s the fixed version. Thanks to Graham Lawrence for the fix. &lt;?php function parse_csv($file,$comma=&#039;,&#039;,$quote=&#039;&quot;&#039;,$newline=&quot;\n&quot;) { $db_quote = $quote . $quote; // Clean up file $file = trim($file); $file = str_replace(&quot;\r\n&quot;,$newline,$file); $file = str_replace($db_quote,&#039;&quot;&#039;,$file); // replace double quotes with &quot; HTML entities $file = str_replace(&#039;,&quot;,&#039;,&#039;,,&#039;,$file); // handle ,&quot;&quot;, empty cells correctly $file = str_replace(&quot;,&quot;\n&quot;,&quot;,\n&quot;,$file); // handle ,&quot;&quot;\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&lt;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,&#039;&#039;,$cell); // Remove delimiter quotes $cell = str_replace(&#039;&quot;&#039;,$quote,$cell); // Add in data quotes if ($row&gt;0) $data[$row][$data[0][$cellNo]] = $cell; else $data[$row][] = $cell; $cellNo++; $start_point = $i + 1; if ($char == $newline) { $cellNo = 0; $row ++; } } } return $data; } --was-- The previous function did not parse empty cells at the end of a line correctly. Here's the fixed version. Thanks to Graham Lawrence for the fix. 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,'&quot;',$file); // replace double quotes with &quot; HTML entities $file = str_replace(',&quot;,',',,',$file); // handle ,"", empty cells correctly $file = str_replace(",&quot;\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('&quot;',$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 (#186296) next »