note 119484 added to function.parse-str
| From: | alxcube at gmail dot com | Date: | Sat, 18 Jun 2016 06:50:13 +0000 |
| Subject: | note 119484 added to function.parse-str | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205751@lists.php.net to get a copy of this message | ||
if you need custom arg separator, you can use this function. it returns parsed query as associative
array.
<?php
/**
* Parses http query string into an array
*
* @author Alxcube <alxcube@gmail.com>
*
* @param string $queryString String to parse
* @param string $argSeparator Query arguments separator
* @param integer $decType Decoding type
* @return array
*/
function http_parse_query($queryString, $argSeparator = '&', $decType =
PHP_QUERY_RFC1738) {
$result = array();
$parts = explode($argSeparator, $queryString);
foreach ($parts as $part) {
list($paramName, $paramValue) = explode('=', $part, 2);
switch ($decType) {
case PHP_QUERY_RFC3986:
$paramName = rawurldecode($paramName);
$paramValue = rawurldecode($paramValue);
break;
case PHP_QUERY_RFC1738:
default:
$paramName = urldecode($paramName);
$paramValue = urldecode($paramValue);
break;
}
if (preg_match_all('/\[([^\]]*)\]/m', $paramName, $matches)) {
$paramName = substr($paramName, 0, strpos($paramName, '['));
$keys = array_merge(array($paramName), $matches[1]);
} else {
$keys = array($paramName);
}
$target = &$result;
foreach ($keys as $index) {
if ($index === '') {
if (isset($target)) {
if (is_array($target)) {
$intKeys = array_filter(array_keys($target),
'is_int');
$index = count($intKeys) ? max($intKeys)+1 : 0;
} else {
$target = array($target);
$index = 1;
}
} else {
$target = array();
$index = 0;
}
} elseif (isset($target[$index]) && !is_array($target[$index])) {
$target[$index] = array($target[$index]);
}
$target = &$target[$index];
}
if (is_array($target)) {
$target[] = $paramValue;
} else {
$target = $paramValue;
}
}
return $result;
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 46.252.214.90
----
Manual Page -- http://php.net/manual/en/function.parse-str.php
Edit -- https://master.php.net/note/edit/119484
Del: integrated -- https://master.php.net/note/delete/119484/integrated
Del: useless -- https://master.php.net/note/delete/119484/useless
Del: bad code -- https://master.php.net/note/delete/119484/bad+code
Del: spam -- https://master.php.net/note/delete/119484/spam
Del: non-english -- https://master.php.net/note/delete/119484/non-english
Del: in docs -- https://master.php.net/note/delete/119484/in+docs
Del: other reasons-- https://master.php.net/note/delete/119484
Reject -- https://master.php.net/note/reject/119484
Search -- https://master.php.net/manage/user-notes.php