lovespring
lovespring

Reputation: 19569

PHP, replace url query var's value

site.com/page1.php?a=1&b=2,how to replace a=1 to a=3? and get a new url string.

Upvotes: 1

Views: 1098

Answers (1)

kevingessner
kevingessner

Reputation: 18985

Use parse_str to extract the values into an array, alter them, and emit with http_build_query:

$query = "a=1&b=2";
parse_str($query, $vals);
$vals['a'] = '3';
$fixed_query = http_build_query($vals);

parse_str docs: http://php.net/manual/en/function.parse-str.php

http_build_query: http://www.php.net/manual/en/function.http-build-query.php

Upvotes: 7

Related Questions