note 123575 rejected from function.explode by cmb
| From: | cmb@php.net | Date: | Thu, 31 Jan 2019 22:03:08 +0000 |
| Subject: | note 123575 rejected from function.explode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210154@lists.php.net to get a copy of this message | ||
Note Submitter: levaconline at gmail dot com
----
Issue with '#' inside string
If you try something like this this you will face with issue:
----------------------------------------------------------------------------
$str = "bill_name2=mabennett&bill_street=1977+sw+peterson+st+#300&bill_city=1";
function parse_q_string(string $q_string = '')
{
if (trim($q_string) == '') {
return [];
}
$result = [];
$particlesPairsArr = explode('&', $q_string);
foreach ($particlesPairsArr as $piece) {
$pair = explode('=', $piece);
if (!empty($pair[0])) {
$result[$pair[0]] = isset($pair[1]) ? $pair[1] : '';
}
}
var_dump( $result );
}
parse_q_string($str);
-----------------------------------------------------------
Result is a broken array by '#' inside the string:
array(4) {
["bill_name2"]=>
string(11) "'mabennett'"
["bill_street"]=>
string(22) "'1977+sw+peterson+st+'"
}