note 90477 added to function.unset
| From: | DavidSawyer at osu1 dot php dot net | Date: | Fri, 24 Apr 2009 04:26:09 +0000 |
| Subject: | note 90477 added to function.unset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-153647@lists.php.net to get a copy of this message | ||
Here's a simple function to remove a variable from an HTTP Query String:
<?php
function unset_query_string_var($varname,$query_string) {
$query_array = array();
parse_str($query_string,$query_array);
unset($query_array[$varname]);
$query_string = http_build_query($query_array);
return $query_string;
}
?>
----------------------------
Example Usage #1:
Let's say you want to remove the variable "fruit" from $page_url below...
<?php
$page_url = "http://www.example.com/script.php?fruit=apple&color=red";
$url_data = parse_url($page_url);
$query_string = unset_query_string_var("fruit",$url_data['query']);
$page_url_new =
$url_data['scheme'].'://'.$url_data['host'].$url_data['path'];
if (!empty($query_string)) $page_url_new .= '?'.$query_string;
$page_url_new .= $url_data['fragment'];
print $page_url_new;
?>
Output:
http://www.example.com/script.php?color=red
----------------------------
Example Usage #2:
To remove the variable "q" from the query string of the current page, try something
like...
<?php
$page_url = 'http://'.$_SERVER["SERVER_NAME"].$_SERVER['PHP_SELF'];
$query_string = unset_query_string_var("q",$_SERVER["QUERY_STRING"]);
if (!empty($query_string)) $page_url .= '?'.$query_string;
?>
----
Server IP: 208.69.120.35
Probable Submitter: 98.216.111.168
----
Manual Page -- http://www.php.net/manual/en/function.unset.php
Edit -- https://master.php.net/note/edit/90477
Del: integrated -- https://master.php.net/note/delete/90477/integrated
Del: useless -- https://master.php.net/note/delete/90477/useless
Del: bad code -- https://master.php.net/note/delete/90477/bad+code
Del: spam -- https://master.php.net/note/delete/90477/spam
Del: non-english -- https://master.php.net/note/delete/90477/non-english
Del: in docs -- https://master.php.net/note/delete/90477/in+docs
Del: other reasons-- https://master.php.net/note/delete/90477
Reject -- https://master.php.net/note/reject/90477
Search -- https://master.php.net/manage/user-notes.php