note 74392 added to function.explode

From: Date: Mon, 09 Apr 2007 19:38:32 +0000
Subject: note 74392 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-125241@lists.php.net to get a copy of this message
For anyone that ever wondered why there isn't a explode backward function, there is now. What makes it different from the php standard explode implementation is the fact that it's limit param is counted from right to left instead of left to right. So for example if you wanted to break up a filename into two pieces: name and extension, you would notice that if the name had a dot in it, the extension would be invalid. Since this function breaks the string up from the back, you can safely set the limit to 2 and have the extension and the filename exploded properly, without worrying about any extra dots. Note: If you don't plan to use the limit parametre, there is no use using this function. Note2: Negative limit functionality is implemented as well. <?php function bexplode ($delimiter, $string, $limit=0) { $strarr = explode($delimiter, $string); $output = array(); $abslimit = abs($limit); for ($i = 0; $i < $abslimit; $i++) { $output[] = array_pop($strarr); } if ($limit > 0) { $output[$limit-1] = implode($delimiter, $strarr) . $delimiter . $output[$limit-1]; } return array_reverse($output); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 87.199.42.58 ---- Manual Page -- http://www.php.net/manual/en/function.explode.php Edit -- https://master.php.net/note/edit/74392 Del: integrated -- https://master.php.net/note/delete/74392/integrated Del: useless -- https://master.php.net/note/delete/74392/useless Del: bad code -- https://master.php.net/note/delete/74392/bad+code Del: spam -- https://master.php.net/note/delete/74392/spam Del: non-english -- https://master.php.net/note/delete/74392/non-english Del: in docs -- https://master.php.net/note/delete/74392/in+docs Del: other reasons-- https://master.php.net/note/delete/74392 Reject -- https://master.php.net/note/reject/74392 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125241) next »