I am trying to grab the URL query of the URL which is in the browser's address bar, then append it at the end of the other URL and echo it to the browser.
So, if I visit my script at:
and the other URL is:
then the browser will display:
This code is quite slow. How can I improve this?
function parse_me($from, &$to) {
// $to = array();
$from = urldecode($from);
$from = urldecode($from);
foreach (explode('&', $from) as $part) {
$part = explode('=', $part);
if ($key = array_shift($part)) {
$to[$key] = implode('', $part);
}
}
// print_r($to);
}
function add_qsrt_to_url($url) {
$other_query_string = arrayGet($_SERVER, 'QUERY_STRING', '');
$url_parsed = parse_url($url);
$new_qs_parsed = array();
if (isset($url_parsed['query'])) {
// parse_str($url_parsed['query'], $new_qs_parsed);
parse_me($url_parsed['query'], $new_qs_parsed);
}
$other_qs_parsed = array();
// parse_str($other_query_string, $other_qs_parsed);
parse_me($other_query_string, $other_qs_parsed);
// print_r($other_qs_parsed);
$final_query_string_array = array_merge($new_qs_parsed, $other_qs_parsed);
// var_dump($final_query_string_array);
$final_query_string = http_build_query($final_query_string_array);
$new_url = $url_parsed['scheme'] . '://' . $url_parsed['host'];
if (isset($url_parsed['path'])) {
$new_url = $new_url . $url_parsed['path'];
}
if ($final_query_string) {
$new_url = $new_url . '?' . $final_query_string;
}
return $new_url;
}
function arrayGet($array, $key, $default = NULL) {
return isset($array[$key]) ? $array[$key] : $default;
}