note 24169 deleted from function.split by jimw
| From: | jimw@php.net | Date: | Thu, 08 Aug 2002 18:54:23 +0000 |
| Subject: | note 24169 deleted from function.split by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-34555@lists.php.net to get a copy of this message | ||
I needed a function to split a query with an delimiter without touching the strings inside.
I wrote the following function to get this:
//this function can split on delimiter without touching strings
function SplitWithString($fStr,$fDelimiter)
{
$cur="";
$quot=0; //single quote '
$dquot=0; //double quote "
for ($i=0;$i<strlen($fStr);$i++)
{
$c=substr($fStr,$i,1);
if ($quot || $dquot)
{
if ($c=="'" && $quot && !$dquot)
{
$quot=0;
} elseif ($c=="\"" && $dquot && !$quot) $dquot=0;
$cur=$cur.$c;
} elseif ($c==$fDelimiter)
{
$rRet[]=$cur;
$cur="";
} else
{
if ($c=="'") $quot=1;
if ($c=="\"") $dquot=1;
$cur=$cur.$c;
}
}
$rRet[]=$cur;
return $rRet;
}
$a="a as b,\"b,z\" as c,'d' as e,\"harhar' sdfsd, '
'\", as d";
$b=SplitWithString($a,",");
print_r($b);
will return:
Array ( [0] => a as b [1] => "b,z" as c [2] => 'd' as e [3] =>
"harhar' sdfsd, ' '" [4] => as d )
Array ( [0] => a as b [1] => "b,z" as c [2] => 'd' as e [3] =>
"jaja' sdfsd, ' '" [4] => as d )