note 99167 deleted from function.explode by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:50:21 +0000 |
| Subject: | note 99167 deleted from function.explode by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215344@lists.php.net to get a copy of this message | ||
Note Submitter: m0gr14 at gmail dot com
----
Here's a function for "multi" exploding a string.
<?php
//the function
//Param 1 has to be an Array
//Param 2 has to be a String
function multiexplode ($delimiters,$string) {
$ary = explode($delimiters[0],$string);
array_shift($delimiters);
if($delimiters != NULL) {
foreach($ary as $key => $val) {
$ary[$key] = multiexplode($delimiters, $val);
}
}
return $ary;
}
// Example of use
$string = "1-2-3|4-5|6:7-8-9-0|1,2:3-4|5";
$delimiters = Array(",",":","|","-");
$res = multiexplode($delimiters,$string);
echo '<pre>';
print_r($res);
echo '</pre>';
//returns
/*
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
)
[2] => Array
(
[0] => 6
)
)
[1] => Array
(
[0] => Array
(
[0] => 7
[1] => 8
[2] => 9
[3] => 0
)
[1] => Array
(
[0] => 1
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[0] => 2
)
)
[1] => Array
(
[0] => Array
(
[0] => 3
[1] => 4
)
[1] => Array
(
[0] => 5
)
)
)
)
*/
?>