note 105061 modified in reserved.variables.get by danbrown

From: Date: Mon, 25 Jul 2011 20:19:56 +0000
Subject: note 105061 modified in reserved.variables.get by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181866@lists.php.net to get a copy of this message
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" ?> --was-- 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.thisIsAnExample.net/index.php?getVar1=Something&getVar2=10&getVar3=ok <?php echo getUrlWithout(array("getVar1","getVar3")); //result will be "http://www.thisIsAnExample.net/index.php?getVar2=10" ?> http://php.net/manual/en/reserved.variables.get.php

« previous php.notes (#181866) next »