note 69899 deleted from function.split by didou
| From: | didou@php.net | Date: | Sun, 07 Jan 2007 19:27:52 +0000 |
| Subject: | note 69899 deleted from function.split by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-121393@lists.php.net to get a copy of this message | ||
Note Submitter: destes at ix dot netcom dot com
----
There is an error with my note dated 14 February 2006. The function as shown does not properly
handle CSV data in the following case:
1,"foo",,"bar"
this should get split to:
{
[0] => 1
[1] => foo
[2] =>
[3] => bar
}
(the blank entry 2 is because of the doubled commas). However, in the function listed, entry 2
would get ignored by the function because it's interpreted as a unitary delimiter and ignored
as containing valid data. Yet it DOES contain valid data, it indicates a blank field value.
To fix this, replace this line in the function:
if ((strlen($val)-$delimlen-$delimlen) >= 1) {
with this:
if ((strlen($val)-$delimlen) >= 1) {
This way, entries at the end of lines or between "foo","bar" will get properly
parsed, but "foo",,"bar" will still be OK.