note 74392 deleted from function.explode by bjori
| From: | bjori@php.net | Date: | Tue, 17 Apr 2007 09:27:28 +0000 |
| Subject: | note 74392 deleted from function.explode by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125498@lists.php.net to get a copy of this message | ||
Note Submitter: blodo at poczta dot fm
----
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);
}
?>