note 105061 deleted from reserved.variables.get by peehaa
| From: | peehaa@php.net | Date: | Sun, 28 Apr 2019 10:59:45 +0000 |
| Subject: | note 105061 deleted from reserved.variables.get by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210851@lists.php.net to get a copy of this message | ||
Note Submitter: Maarten Schroeven
----
You can use this function to remove any $_GET variables out of your URL, it takes an array off
strings(the names keys of the $_GET you wish to remove) and returns the url with the ones specified
removed
<?php
function getUrlWithout($getNames){
$url = "http" . ((!empty($_SERVER['HTTPS'])) ? "s" : "")
. "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$questionMarkExp = explode("?", $url);
$urlArray = explode("&", $questionMarkExp[1]);
$retUrl=$questionMarkExp[0];
$retGet="";
$found=array();
foreach($getNames as $id => $name){
foreach ($urlArray as $key=>$value){
if(isset($_GET[$name]) && $value==$name."=".$_GET[$name])
unset($urlArray[$key]);
}
}
$urlArray = array_values($urlArray);
foreach ($urlArray as $key => $value){
if($key<sizeof($urlArray) && $retGet!=="")
$retGet.="&";
$retGet.=$value;
}
return $retUrl."?".$retGet;
}
?>
Example
current url is http://www.example.net/index.php?getVar1=Something&getVar2=10&getVar3=ok
<?php
echo getUrlWithout(array("getVar1","getVar3"));
//result will be "http://www.example.net/index.php?getVar2=10"
?>