note 28780 added to function.explode
| From: | mtcrowe at jupiter dot wox dot org | Date: | Thu, 23 Jan 2003 17:53:16 +0000 |
| Subject: | note 28780 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-42655@lists.php.net to get a copy of this message | ||
I've run across certain CSV (comma delimited) files that do not follow the standard "quote
the sections that have commas as part of the string" formatting. Instead, these files escape
the commas that are part of the field with a backslash.
Here's some code to explode a CSV string formatted in such a way into an array:
function csv_explode($expldchar, $str)
{
if(strlen($expldchar) != 1 || strlen($str) == 0)
{
return 0;
}
$idx=0;
$arr=0;
while($idx < strlen($str))
{
if($str[$idx] == '\\')
{
$idx+=2;
$newstrarr[$arr] = $newstrarr[$arr] . $str[$idx];
}
elseif($str[$idx] == ',') $arr++;
else $newstrarr[$arr] = $newstrarr[$arr] . $str[$idx];
$idx++;
}
return $newstrarr;
}
(Disclaimer: I'm not a professional programmer and, more than likely, improvements and
efficiencies can be made to the code segment above.)
--
http://www.php.net/manual/en/function.explode.php
http://master.php.net/manage/user-notes.php?action=edit+28780
http://master.php.net/manage/user-notes.php?action=delete+28780
http://master.php.net/manage/user-notes.php?action=reject+28780