views:

27

answers:

1

hi guys,

I am given a page url like 'http://abc.com/test.php?a=1&b=2&c=3'. Now I have been told to change the value of b to 5 so that it becomes 'http://abc.com/test.php?a=1&b=5&c=3'.

i.e change from http://abc.com/test.php?a=1&b=2&c=3 to http://abc.com/test.php?a=1&b=5&c=3

Note: variable b here can refer to any name.

+5  A: 

Use

  • parse_url() to extract the query string from the URL

  • parse_str() to split the query string into an array

  • array_merge() to add a new array "b" => 5

  • http_build_query() to re-build a query string

  • The remaining parts from the first step (protocol, host, path...) to re-build the full URL

Pekka
+1, indeed the way to go, but if you have the `HTTP` pecl extension,a [`http_build_url()`](http://php.net/manual/en/function.http-build-url.php) with [`HTTP_URL_JOIN_QUERY`](http://www.php.net/manual/en/http.constants.php) will alleviate much of the work.
Wrikken
@Wrikken nice, good to know!
Pekka