3
\$\begingroup\$

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:

http://script.com/script.php?a=x&b=z

and the other URL is:

http://google.com/?c=v&d=u

then the browser will display:

http://google.com?a=x&b=z&c=v&d=u

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;
}
asked Feb 10, 2016 at 13:35
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Are they typos or are you using spaces after every dollar sign for real? \$\endgroup\$ Commented Feb 10, 2016 at 15:09
  • \$\begingroup\$ Just out of curiousity, why do you break '?a=x&b=y' into a hash with two keys? Why not just copy the query string as is without parsing it? \$\endgroup\$ Commented Feb 11, 2016 at 22:14
  • \$\begingroup\$ @BarryCarter, because I want to merge its URL query with the existing URL query with of the other URL. \$\endgroup\$ Commented Feb 12, 2016 at 4:59
  • \$\begingroup\$ Oh, OK. Suggest you give a second URL more like "google.com/?c=v&d=u" so we can see the merging. \$\endgroup\$ Commented Feb 12, 2016 at 5:06
  • \$\begingroup\$ @BarryCarter, good suggestion, updated. Thanks! \$\endgroup\$ Commented Feb 12, 2016 at 5:29

1 Answer 1

2
+100
\$\begingroup\$

I'd write it like this

function concatQueryVars($first, $second) {
 $url = strtok($first, '?');
 $first = parse_url($first, PHP_URL_QUERY);
 $second = parse_url($second, PHP_URL_QUERY);
 return !empty($url) && 
 !empty($first) && 
 !empty($second) 
 ? $url . '?' . $first. '&' . $second : false;
}
echo concatQueryVars('http://example.com/?a=b', 'http://example.com/?c=d&e=f');

Result:

http://example.com/?a=b&c=d&e=f

answered Feb 12, 2016 at 14:48
\$\endgroup\$
1
  • \$\begingroup\$ I was going to suggest replacing the '?' in the script URL with an '&', but that wouldn't work in all cases. Your solution is better. \$\endgroup\$ Commented Feb 12, 2016 at 14:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.