美丽的方式来删除与PHP的GETvariables?
我有一个包含GETvariables的完整URL的string。 哪个是删除GETvariables的最好方法? 有没有一个很好的方法来删除其中的一个?
这是一个代码,但不是很漂亮(我认为):
$current_url = explode('?', $current_url); echo $current_url[0];
上面的代码只是删除所有的GETvariables。 URL在我的情况下是从CMS生成的,所以我不需要关于服务器variables的任何信息。
好吧,要删除所有variables,也许最漂亮的是
$url = strtok($url, '?');
在这里看到strtok
。
它是最快的(见下文),并处理没有“?”的url。 正常。
要获取一个url + querystring并只删除一个variables(不使用正则expression式replace,在某些情况下可能会更快),您可以执行如下操作:
function removeqsvar($url, $varname) { list($urlpart, $qspart) = array_pad(explode('?', $url), 2, ''); parse_str($qspart, $qsvars); @unset($qsvars[$varname]); $newqs = http_build_query($qsvars); return $urlpart . '?' . $newqs; }
正则expression式replace去除单个var可能看起来像:
function removeqsvar($url, $varname) { return preg_replace('/([?&])'.$varname.'=[^&]+(&|$)/','$1',$url); }
下面介绍几种不同方法的时间安排,确保时间间隔在运行间重置。
<?php $number_of_tests = 40000; $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; preg_replace('/\\?.*/', '', $str); } $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "regexp execution time: ".$totaltime." seconds; "; $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; $str = explode('?', $str); } $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "explode execution time: ".$totaltime." seconds; "; $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; $qPos = strpos($str, "?"); $url_without_query_string = substr($str, 0, $qPos); } $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "strpos execution time: ".$totaltime." seconds; "; $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; $url_without_query_string = strtok($str, '?'); } $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "tok execution time: ".$totaltime." seconds; ";
节目
regexp execution time: 0.14604902267456 seconds; explode execution time: 0.068033933639526 seconds; strpos execution time: 0.064775943756104 seconds; tok execution time: 0.045819044113159 seconds; regexp execution time: 0.1408839225769 seconds; explode execution time: 0.06751012802124 seconds; strpos execution time: 0.064877986907959 seconds; tok execution time: 0.047760963439941 seconds; regexp execution time: 0.14162802696228 seconds; explode execution time: 0.065848112106323 seconds; strpos execution time: 0.064821004867554 seconds; tok execution time: 0.041788101196289 seconds; regexp execution time: 0.14043688774109 seconds; explode execution time: 0.066350221633911 seconds; strpos execution time: 0.066242933273315 seconds; tok execution time: 0.041517972946167 seconds; regexp execution time: 0.14228296279907 seconds; explode execution time: 0.06665301322937 seconds; strpos execution time: 0.063700199127197 seconds; tok execution time: 0.041836977005005 seconds;
strtok胜,是迄今为止最小的代码。
怎么样:
preg_replace('/\\?.*/', '', $str)
受@MitMaro评论的启发,我写了一个小的基准来testing@Gumbo,@Matt Bridges和@justin解决scheme的速度:
function teststrtok($number_of_tests){ for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; $str = strtok($str,'?'); } } function testexplode($number_of_tests){ for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; $str = explode('?', $str); } } function testregexp($number_of_tests){ for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; preg_replace('/\\?.*/', '', $str); } } function teststrpos($number_of_tests){ for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; $qPos = strpos($str, "?"); $url_without_query_string = substr($str, 0, $qPos); } } $number_of_runs = 10; for($runs = 0; $runs < $number_of_runs; $runs++){ $number_of_tests = 40000; $functions = array("strtok", "explode", "regexp", "strpos"); foreach($functions as $func){ $starttime = microtime(true); call_user_func("test".$func, $number_of_tests); echo $func.": ". sprintf("%0.2f",microtime(true) - $starttime).";"; } echo "<br />"; }
strtok:0.12;爆炸:0.19;正则expression式:0.31; strpos:0.18; strtok:0.12;爆炸:0.19;正则expression式:0.31; strpos:0.18; strtok:0.12;爆炸:0.19;正则expression式:0.31; strpos:0.18; strtok:0.12;爆炸:0.19;正则expression式:0.31; strpos:0.18; strtok:0.12;爆炸:0.19;正则expression式:0.31; strpos:0.18; strtok:0.12;爆炸:0.19;正则expression式:0.31; strpos:0.18; strtok:0.12;爆炸:0.19;正则expression式:0.31; strpos:0.18; strtok:0.12;爆炸:0.19;正则expression式:0.31; strpos:0.18; strtok:0.12;爆炸:0.19;正则expression式:0.31; strpos:0.18; strtok:0.12;爆炸:0.19;正则expression式:0.31; strpos:0.18;
结果:@贾斯汀的strtok是最快的。
注意:使用Apache2和PHP5在本地Debian Lenny系统上进行testing。
如果您试图从中删除查询string的URL是PHP脚本的当前URL,则可以使用前面提到的方法之一。 如果你只是有一个URL的stringvariables,你想剥离过去的'?' 你可以做:
$pos = strpos($url, "?"); $url = substr($url, 0, $pos);
另一种解决scheme…我发现这个function更优雅,它也将删除尾随'?' 如果要删除的键是查询string中唯一的一个。
/** * Remove a query string parameter from an URL. * * @param string $url * @param string $varname * * @return string */ function removeQueryStringParameter($url, $varname) { $parsedUrl = parse_url($url); $query = array(); if (isset($parsedUrl['query'])) { parse_str($parsedUrl['query'], $query); unset($query[$varname]); } $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : ''; $query = !empty($query) ? '?'. http_build_query($query) : ''; return $parsedUrl['scheme']. '://'. $parsedUrl['host']. $path. $query; }
testing:
$urls = array( 'http://www.example.com?test=test', 'http://www.example.com?bar=foo&test=test2&foo2=dooh', 'http://www.example.com', 'http://www.example.com?foo=bar', 'http://www.example.com/test/no-empty-path/?foo=bar&test=test5', 'https://www.example.com/test/test.test?test=test6', ); foreach ($urls as $url) { echo $url. '<br/>'; echo removeQueryStringParameter($url, 'test'). '<br/><br/>'; }
会输出:
http://www.example.com?test=test http://www.example.com http://www.example.com?bar=foo&test=test2&foo2=dooh http://www.example.com?bar=foo&foo2=dooh http://www.example.com http://www.example.com http://www.example.com?foo=bar http://www.example.com?foo=bar http://www.example.com/test/no-empty-path/?foo=bar&test=test5 http://www.example.com/test/no-empty-path/?foo=bar https://www.example.com/test/test.test?test=test6 https://www.example.com/test/test.test
»在3v41上运行这些testing
你可以使用这个服务器variables ,例如$_SERVER['REQUEST_URI']
,甚至更好: $_SERVER['PHP_SELF']
。
你不能使用服务器variables来做到这一点?
或者这会工作吗?
unset($_GET['page']); $url = $_SERVER['SCRIPT_NAME'] ."?".http_build_query($_GET);
只是一个想法。
如何通过循环$ _GET数组来重写查询string的函数
! 一个合适的function粗糙的轮廓
function query_string_exclude($exclude, $subject = $_GET, $array_prefix=''){ $query_params = array; foreach($subject as $key=>$var){ if(!in_array($key,$exclude)){ if(is_array($var)){ //recursive call into sub array $query_params[] = query_string_exclude($exclude, $var, $array_prefix.'['.$key.']'); }else{ $query_params[] = (!empty($array_prefix)?$array_prefix.'['.$key.']':$key).'='.$var; } } } return implode('&',$query_params); }
像这样的东西可以很好地保持分页链接等的便利。
<a href="?p=3&<?= query_string_exclude(array('p')) ?>" title="Click for page 3">Page 3</a>
@list($url) = explode("?", $url, 2);
basename($_SERVER['REQUEST_URI'])
返回包含'?'之后的所有内容,
在我的代码有时我只需要部分,所以分开它,所以我可以得到我所需要的价值。 与其他方法相比,不能确定性能的速度,但对我来说真的很有用。
$urlprotocol = 'http'; if ($_SERVER["HTTPS"] == "on") {$urlprotocol .= "s";} $urlprotocol .= "://"; $urldomain = $_SERVER["SERVER_NAME"]; $urluri = $_SERVER['REQUEST_URI']; $urlvars = basename($urluri); $urlpath = str_replace($urlvars,"",$urluri); $urlfull = $urlprotocol . $urldomain . $urlpath . $urlvars;
在我看来,最好的办法是这样的:
<? if(isset($_GET['i'])){unset($_GET['i']); header('location:/');} ?>
它检查是否有一个'我'的GET参数,并删除它,如果有。