note 103002 deleted from function.explode by danbrown
| From: | danbrown@php.net | Date: | Sat, 19 Mar 2011 15:23:09 +0000 |
| Subject: | note 103002 deleted from function.explode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178006@lists.php.net to get a copy of this message | ||
Note Submitter: Kamba Jeno
----
if u cannot use the original function explode, or your version is less than 4.0.1 you can use this
example whith optional limit argument.
<?php
function exploding($separator,$string,$limit=NULL){
$returnarray = array();
$increment = 0;
$length = strlen($string);
$limit_negative=($limit < 0);
for($i = 0; $i < $length;$i++){
$current = substr($string,$i,1);
//main explode function
if($separator == $current){
$increment++;
}
elseif($separator == ""){
@$returnarray[$increment].=$current;
$increment++;
}
else{
@$returnarray[$increment].=$current;
}
//if limit is positive
if($limit == $increment){
break;
}
}
//if limit is negative
if($limit_negative){
for($i = 0; $i > $limit;$i--){
array_pop($returnarray);
}
}
return $returnarray;
}
//Test
$something = "Kamba.jeno.attila";
$result = exploding(".",$something)
/*returns
array(3) {
[0]=> string(5) "Kamba"
[1]=> string(4) "Jeno"
[2]=> string(6) "Attila" }
*/
$result2 = exploding(".",$something,1)
/*returns
array(1) {
[0]=> string(5) "Kamba" }
*/
$result3 = exploding(".",$something,-1)
/*returns
array(2) {
[0]=> string(5) "Kamba"
[1]=> string(4) "Jeno" }
*/
?>