note 93324 deleted from function.explode by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:50:24 +0000 |
| Subject: | note 93324 deleted from function.explode by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215345@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
<?php
// converts pure string into a trimmed keyed array
function string2KeyedArray($string, $delimiter = ',', $kv = '=>') {
if ($a = explode($delimiter, $string)) { // create parts
foreach ($a as $s) { // each part
if ($s) {
if ($pos = strpos($s, $kv)) { // key/value delimiter
$ka[trim(substr($s, 0, $pos))] = trim(substr($s, $pos + strlen($kv)));
} else { // key delimiter not found
$ka[] = trim($s);
}
}
}
return $ka;
}
} // string2KeyedArray
$string = 'a=>1, b=>23 , $a, c=> 45% , true,d => ab c ';
print_r(string2KeyedArray($string));
?>
Array
(
[a] => 1
[b] => 23
[0] => $a
[c] => 45%
[1] => true
[d] => ab c
)