note 100616 deleted from function.explode by danbrown
| From: | danbrown@php.net | Date: | Wed, 27 Oct 2010 04:10:34 +0000 |
| Subject: | note 100616 deleted from function.explode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173491@lists.php.net to get a copy of this message | ||
Note Submitter: ben at hydroprofessional dot com
----
Extension on the multiExplode function; allows the usage of keys instead of an incrementing number.
Feel free to simplify it.
<?php
function multiExplode($a, $s, $k=false, $ks="::") {
$r = explode($a[0], $s);
if($k) {
$nr = array();
$set = false;
for($i = 0; $i < count($r); $i++) {
$sp = strpos($r[$i], $ks);
if($sp !== false) {
$set = true;
$nr[strstr($r[$i],$ks,true)] = substr(strstr($r[$i],$ks),2);
} else {
$nr[] = $r[$i];
}
}
if($set) $r = $nr;
}
array_shift($a);
if(isset($a[0])) foreach($r as $key => $val) if(strpos($val, $a[0]) !== false || strpos($val,
$ks) !== false) $r[$key] = multiExplode($a, $val, $k, $ks);
return $r;
}
echo "<pre>";
print_r(multiExplode(array("||","[]"), "Key 1::Key 1 Data||Key 2::Key 2
Information||Key 3::Key 1::Lorem[]Key 2:Ipsum||Hello||World", true));
echo "</pre>";
/*
Outputs:
Array
(
[Key 1] => Key 1 Data
[Key 2] => Key 2 Information
[Key 3] => Array
(
[Key 1] => Lorem
[0] => Key 2:Ipsum
)
[0] => Hello
[1] => World
)
*/
?>