I've looked at every post on SO that remotely pertains to this and I just can't figure this out. This code is taken directly from another SO post and was marked as the correct working answer:
$query = $_GET;
// replace parameter(s)
$query['d'] = 'new_value';
// rebuild url
$query_result = http_build_query($query);
// new link
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_result; ?>">Link</a>
Again, taken straight from another post. When I try this code, i change the $_GET
to the actual URL that i want to alter. When the code gets to the $query['d']
part, it tells me I get an illegal string offset
and the error is the index that's specified. So then I parse the URL, and then do parse_str($query, $output)
which in turn allows me to do $output['d']
and THEN I can set a new value to that variable. If I echo it out, it's fine.
But then I get to the http_build_query
line, and it tells me that it's expecting an array or object and I can't build the new URL. Here is my code:
$link = parse_url('https://www.google.com/search?source=hp&ei=85GhW6CNHoSqsgXnzoD4Ag&q=coding+tutorial&btnK=Google+Search&oq=coding+tutorial', PHP_URL_QUERY);
parse_str($link, $output);
$output['oq'] = 'new value';
$query_result = http_build_query($link);
echo $query_result;
This code yields that the http_build_query
function wants an array or object...i guess i'm not giving it that in some way? What do I need to do to get this to work?