note 89969 deleted from function.explode by googleguy
| From: | googleguy@php.net | Date: | Thu, 22 Nov 2012 16:59:53 +0000 |
| Subject: | note 89969 deleted from function.explode by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192099@lists.php.net to get a copy of this message | ||
Note Submitter: Navi
----
<?php
function my_explode($delim, $str, $lim = 1)
{
if ($lim > -2) return explode($delim, $str, abs($lim));
$lim = -$lim;
$out = explode($delim, $str);
if ($lim >= count($out)) return $out;
$out = array_chunk($out, count($out) - $lim + 1);
return array_merge(array(implode($delim, $out[0])), $out[1]);
}
?>
This function can assume `limit' parameter less than 0, for example:
<?php
print_r(my_explode('.', 'file.some.ext.jpg', -2));
?>
prints
Array
(
[0] => file.some.ext
[1] => jpg
)