note 100616 added to function.explode
| From: | ben at hydroprofessional dot com | Date: | Wed, 27 Oct 2010 04:05:03 +0000 |
| Subject: | note 100616 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173488@lists.php.net to get a copy of this message | ||
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
)
*/
?>
----
Server IP: 69.147.83.197
Probable Submitter: 115.64.165.37
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- https://master.php.net/note/edit/100616
Del: integrated -- https://master.php.net/note/delete/100616/integrated
Del: useless -- https://master.php.net/note/delete/100616/useless
Del: bad code -- https://master.php.net/note/delete/100616/bad+code
Del: spam -- https://master.php.net/note/delete/100616/spam
Del: non-english -- https://master.php.net/note/delete/100616/non-english
Del: in docs -- https://master.php.net/note/delete/100616/in+docs
Del: other reasons-- https://master.php.net/note/delete/100616
Reject -- https://master.php.net/note/reject/100616
Search -- https://master.php.net/manage/user-notes.php