note 43168 added to function.explode
| From: | shimon at schoolportal dot co dot il | Date: | Fri, 11 Jun 2004 19:35:44 +0000 |
| Subject: | note 43168 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-70940@lists.php.net to get a copy of this message | ||
<?
// explode simple multi-imploded arrays
// supports: single quoted+backslashed strings and [] arrays
// example: $var=multi_explode1("['a',['b','c']]");
function multi_explode1($s) // Author: Shimon Doodkin
{
$mystack=array(false);
$mytop=0;
$l=strlen($s);
$instring=false;
for($i=0;$i<$l;$i++)
{
if($instring) // string loop
{
if($s[$i]=='\\') // \' unescape
{
$i++;
$mystack[$mytop].=$s[$i];
}
else
if($s[$i]=='\'') // end string
{
$mystack[$mytop-1][]=$mystack[$mytop];
unset($mystack[$mytop]);
$mytop--;
$instring=false;
}
else
{
$mystack[$mytop].=$s[$i];
}
}
else // main loop
{
if($s[$i]=='[') // start array
{
$mytop++;
$mystack[$mytop]=array();
}
else
if($s[$i]=='\'') // start string
{
$mytop++;
$mystack[$mytop]='';
$instring=true;
}
else
if($s[$i]==']') // end array
{
$mystack[$mytop-1][]=$mystack[$mytop];
unset($mystack[$mytop]);
$mytop--;
}
}
}
return $mystack[0][0];
}
?>
<?
// explode complex multi-imploded arrays
// supports: numbers and single quoted + double quoted +
// backslashed strings and [] arrays and a comma separator
// can parse: ['a', 1, 2.33 ,['a',"a\'a\r\na\""]]
// example:
// $var=multi_explode("['a', 1,2.3,
['a','a\\'a\r\na\\\"\"]]");
// should return: Array('a', 1, 2.33,
Array('a',"a\'a\r\na\""])
//
function multi_explode($s) // Author: Shimon Doodkin
{
$mystack=array(false);
$mytop=0;
$l=strlen($s);
$instring1=false;
$instring2=false;
$innumber=false;
for($i=0;$i<$l;$i++)
{
if($innumber) // number loop
{
// array end + unexpected end + seperator - end number
if($s[$i]==','||$s[$i]==']'||$i+1==$l)
{
$mystack[$mytop]=rtrim($mystack[$mytop]);
if(is_numeric ($mystack[$mytop]) ?
intval(0+$mystack[$mytop] ) == $mystack[$mytop] : false )
{
$mystack[$mytop-1][]=intval($mystack[$mytop]);
}
else
{
$mystack[$mytop-1][]=floatval($mystack[$mytop]);
}
unset($mystack[$mytop]);
$mytop--;
$innumber=false;
if($s[$i]==']')$i--;
}
else
{
$mystack[$mytop].=$s[$i];
}
}
else
if($instring1) // single quot string loop
{
if($s[$i]=='\\') // unescape
{
$i++;
switch($s[$i])
{
case 'n':
$mystack[$mytop].="\n";
break;
case 'r':
$mystack[$mytop].="\r";
break;
case 't':
$mystack[$mytop].="\t";
break;
default: // case ' " \
$mystack[$mytop].=$s[$i];
}
}
else
if($s[$i]=='\'') // end string
{
$mystack[$mytop-1][]=$mystack[$mytop];
unset($mystack[$mytop]);
$mytop--;
$instring1=false;
}
else
{
$mystack[$mytop].=$s[$i];
}
}
else
if($instring2) // double quot string loop
{
if($s[$i]=='\\') // unescape
{
$i++;
switch($s[$i])
{
case 'n':
$mystack[$mytop].="\n";
break;
case 'r':
$mystack[$mytop].="\r";
break;
case 't':
$mystack[$mytop].="\t";
break;
default: // case ' " \
$mystack[$mytop].=$s[$i];
}
}
else
if($s[$i]=='"') // end string
{
$mystack[$mytop-1][]=$mystack[$mytop];
unset($mystack[$mytop]);
$mytop--;
$instring2=false;
}
else
{
$mystack[$mytop].=$s[$i];
}
}
else // main loop
{
if($s[$i]=='[') // start array
{
$mytop++;
$mystack[$mytop]=array();
}
else
if($s[$i]=='\'') // start string
{
$mytop++;
$mystack[$mytop]='';
$instring1=true;
}
else
if($s[$i]=='"') // start string
{
$mytop++;
$mystack[$mytop]='';
$instring2=true;
}
else
if($s[$i]==']') // end array
{
$mystack[$mytop-1][]=$mystack[$mytop];
unset($mystack[$mytop]);
$mytop--;
}
else
if($s[$i]==',') // seperator - nothing to do
{
// only for skipping is_numeric
}
else
if(is_numeric($s[$i])) // start number
{
$mytop++;
$mystack[$mytop]=$s[$i];
$innumber=true;
}
}
}
return $mystack[0][0];
}
?>
Escaping a string in javascript example:
<script language="javascript">
mystring .replace(/\\/, "\\") .replace(/'/, '\'') .replace(/\n/,
'\\n') .replace(/\r/, '\\r');
</script>
made for you by CNSe support.
CNSe: SchoolPortal - CMS , MultiSeker - new age and full of features online questionnaire with
realtime reporting
cnse@schoolportal.co.il
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43168
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43168&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43168&report=yes
Search -- http://master.php.net/manage/user-notes.php