note 71278 deleted from function.explode by colder
| From: | colder@php.net | Date: | Tue, 21 Nov 2006 09:28:55 +0000 |
| Subject: | note 71278 deleted from function.explode by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-119922@lists.php.net to get a copy of this message | ||
Note Submitter: sstaples at thestapler dot ca
----
I needed an explode funciton that would ignore commas inside a set of double quotes, in which I
found that PHP ignores. So I wrote this litle fucntion that would do just that.
I know that EXCEL places strings into " " fields, and numbers without, which really
screwed things up.
I hope this helps for those of you out there that may have actually thought that your CSV files
worked, and may not actually work... until you find it too late!
<?php
function explodeWithString($delimiter, $line)
{
$line = trim($line);
$lineLen = strlen($line);
$arrayCntr = 0;
$string = false;
for($i = 0; $i < $lineLen; $i++)
{
if($line{$i} == '"' && $string == false)
{
$string = true;
continue;
}
elseif($line{$i} == '"' && $string == true)
{
$string = false;
continue;
}
elseif($line{$i} == $delimiter && $string == false)
{
$arrayCntr++;
continue;
}
$array[$arrayCntr] .= $line{$i};
}
return $array;
}
?>