Remove a value from a querystring
I have a URL in which a querystring is produced by a PHP script. Various values are displayed in the querystring.
Basically, I need to remove a specific value from the query string when a visitor clicks on a link or a 'remove' button.
So, the querystring looks like this:
http://www.foo.com/script.php?bar1=green&bar2=blue
But when a link or 'remove' button is clicked by a user, bar1=green
is removed, and the visitor is directed to the following URL:
http://www.foo.com/script.php?bar2=blue
I thought this would be easy using basic HTML with a form or anchor but I haven't been able to do it so far.
Just so you know, i do not have access to the code on the PHP script itself; it is hosted remotely and is called to my webpa开发者_如何学编程ge by a PHP wrapper using an iframe.
Any suggestions greatly appreciated.
Many thanks,
Matt
You can remove the value from the query string using this code:
<?php
function parseQueryString($url,$remove) {
$infos=parse_url($url);
$str=$infos["query"];
$op = array();
$pairs = explode("&", $str);
foreach ($pairs as $pair) {
list($k, $v) = array_map("urldecode", explode("=", $pair));
$op[$k] = $v;
}
if(isset($op[$remove])){
unset($op[$remove]);
}
return str_replace($str,http_build_query($op),$url);
}
echo parseQueryString( "http://www.foo.com/script.php?bar1=green&bar2=blue","bar2");
?>
精彩评论