PHP Functions for parsing & building query strings, in a better way than http_build_query or parse_str does.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function ParseQueryString($query, $orig = false) { | |
$items = $orig ? $orig : array(); | |
$_items = explode('&', $query); | |
foreach ($_items as $item) { | |
if (strpos($item, '=')===false) { | |
$items[urldecode($item)] = true; | |
} | |
else { | |
$itemp = explode('=', $item); | |
$key = urldecode($itemp[0]); | |
if (substr($key, -2)=='[]') | |
$items[substr($key, 0, -2)][] = urldecode($itemp[1]); | |
else | |
$items[$key] = urldecode($itemp[1]); | |
} | |
} | |
return $items; | |
} | |
function BuildQueryString($items) { | |
$queryItems = array(); | |
foreach ($items as $name => $val) { | |
if ($val===false) { | |
continue; | |
} elseif ($val===true) { | |
$queryItems[] = urlencode($name); | |
} elseif (is_array($val)) { | |
foreach ($val as $_val) { | |
$queryItems[] = urlencode($name).'[]='.urlencode($_val); | |
} | |
} else { | |
$queryItems[] = urlencode($name).'='.urlencode($val); | |
} | |
} | |
return implode('&', $queryItems); | |
} | |
function AppendQueryString($url, $queryValues) { | |
$_qMarkPos = strpos($url, '?'); | |
$existingQuery = $_qMarkPos === false ? false : substr($url, $_qMarkPos + 1); | |
$baseURL = $_qMarkPos !== false ? substr($url, 0, $_qMarkPos) : $url; | |
$items = array(); | |
if ($existingQuery) | |
$items = ParseQueryString($existingQuery); | |
$queryValues = is_array($queryValues) ? BuildQueryString($queryValues) : $queryValues; | |
$items = ParseQueryString($queryValues, $items); | |
return $baseURL . '?' . BuildQueryString($items); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment