note 45450 added to function.explode
| From: | mfoss at NOSPAM dot ptd dot net | Date: | Tue, 07 Sep 2004 20:35:35 +0000 |
| Subject: | note 45450 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-76166@lists.php.net to get a copy of this message | ||
Oddly enough, there is not a way to chose which direction a string is parsed in when exploding - it
always goes from left to right. For example, the following code
<?php
explode('&&&', 'name1=Bonnie &&&&name2=Clyde');
?>
will result in returning
<?php
array('name1=Bonnie ', '&name2=Clyde')
?>
when what we really want is
<?php
array('name1=Bonnie &', 'name2=Clyde')
?>
To correctly obtain this value, reverse the string before the explosion, then reverse the individual
array results after it is done, like so:
<?php
$params = 'name1=Bonnie &&&&name2=Clyde';
$s = strrev($params); // 'edylC=2eman&&&& einnoB=1eman'
$arr = explode('&&&', $s); // array('edylC=2eman', '&
einnoB=1eman')
for ($i = 0; $i < count($arr); $i++)
$arr[$i] = strrev($arr[$i]);
// ('name2=Clyde', 'name1=Bonnie &')
/*
Of course, you will also have to reverse
the order of the array as well:
*/
$arr = array_reverse($arr);
?>
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45450
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45450&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45450&report=yes
Search -- http://master.php.net/manage/user-notes.php