note 103002 added to function.explode
| From: | KambaJeno at osu1 dot php dot net | Date: | Sat, 19 Mar 2011 13:36:11 +0000 |
| Subject: | note 103002 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178003@lists.php.net to get a copy of this message | ||
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" }
*/
?>
----
Server IP: 69.147.83.197
Probable Submitter: 89.133.78.126
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- https://master.php.net/note/edit/103002
Del: integrated -- https://master.php.net/note/delete/103002/integrated
Del: useless -- https://master.php.net/note/delete/103002/useless
Del: bad code -- https://master.php.net/note/delete/103002/bad+code
Del: spam -- https://master.php.net/note/delete/103002/spam
Del: non-english -- https://master.php.net/note/delete/103002/non-english
Del: in docs -- https://master.php.net/note/delete/103002/in+docs
Del: other reasons-- https://master.php.net/note/delete/103002
Reject -- https://master.php.net/note/reject/103002
Search -- https://master.php.net/manage/user-notes.php