note 45450 deleted from function.explode by tomsommer

From: Date: Tue, 07 Sep 2004 21:48:36 +0000
Subject: note 45450 deleted from function.explode by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-76172@lists.php.net to get a copy of this message
Note Submitter: mfoss at NOSPAM dot ptd dot net ---- 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); ?>

« previous php.notes (#76172) next »