note 54306 added to function.split
| From: | robin at teddyb dot org | Date: | Thu, 30 Jun 2005 04:01:57 +0000 |
| Subject: | note 54306 added to function.split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-91172@lists.php.net to get a copy of this message | ||
Actually, this version is better than the last I submitted. The goal here is to be able to engage
in *multiple* delimeter removal passes; for all but the last pass, set the third value to
"1", and everything should go well.
function quotesplit( $splitter=',', $s, $restore_quotes=0 )
{
//First step is to split it up into the bits that are surrounded by quotes
//and the bits that aren't. Adding the delimiter to the ends simplifies
//the logic further down
$getstrings = explode('"', $splitter.$s.$splitter);
//$instring toggles so we know if we are in a quoted string or not
$delimlen = strlen($splitter);
$instring = 0;
while (list($arg, $val) = each($getstrings))
{
if ($instring==1)
{
if( $restore_quotes )
{
//Add the whole string, untouched to the previous value in the array
$result[count($result)-1] =
$result[count($result)-1].'"'.$val.'"';
} else {
//Add the whole string, untouched to the array
$result[] = $val;
}
$instring = 0;
}
else
{
//Break up the string according to the delimiter character
//Each string has extraneous delimiters around it (inc the ones we added
//above), so they need to be stripped off
$temparray = split($splitter, substr($val, $delimlen,
strlen($val)-$delimlen-$delimlen+1 ) );
while(list($iarg, $ival) = each($temparray))
{
$result[] = trim($ival);
}
$instring = 1;
}
}
return $result;
}
----
Manual Page -- http://www.php.net/manual/en/function.split.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54306
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54306&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54306&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54306&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54306&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54306&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54306&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54306&report=yes
Search -- http://master.php.net/manage/user-notes.php