In short, you simply parse the URL and then add the parameter at the end or replace it if it already exists.
$parts = parse_url($url) + array('query' => array()); parse_str($parts['query'], $query); $query['page'] = $page; $parts['query'] = http_build_str($query); $newUrl = http_build_url($parts);
This code sample requires the PHP HTTP module for http_build_url
and http_build_str
. Later, you can replace http_build_query
, and for the first, implement the PHP user space if you do not have the module installed.
Another alternative is to use the Net_URL2
package, which offers an interface for various URL operations:
$op = new Net_URL2($url); $op->setQueryVariable('page', $page); $newUrl = (string) $op;
It is more flexible and expressive.
hakre source share