note 51802 added to function.explode
| From: | richardkmiller at gmail dot com | Date: | Mon, 11 Apr 2005 19:01:22 +0000 |
| Subject: | note 51802 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-87853@lists.php.net to get a copy of this message | ||
The function posted by tengel at fluid dot com didn't work for me -- it wouldn't compile.
Here is the function I wrote to do the same thing. This function explodes a string, ignoring
delimeters that appear inside double quotes. (Incidentally, it also removes double quotes from the
exploded elements.)
This correctly parses, for example, a line from a CSV file like this:
10, "abc", "ACME, Inc.", 50.25
It's slow, but it works. (How could this be faster?)
function explode2($delimeter, $string)
{
for ($i = 0; $i < strlen($string); $i++)
{
if ($string{$i} == '"')
{
if ($insidequotes)
$insidequotes = false;
else
$insidequotes = true;
}
elseif ($string{$i} == $delimeter)
{
if ($insidequotes)
{
$currentelement .= $string{$i};
}
else
{
$returnarray[$elementcount++] = $currentelement;
$currentelement = '';
}
}
else
{
$currentelement .= $string{$i};
}
}
$returnarray[$elementcount++] = $currentelement;
return $returnarray;
}
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51802
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51802&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51802&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51802&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51802&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51802&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51802&report=yes
Search -- http://master.php.net/manage/user-notes.php