美丽的方式来删除PHP的GET变量? [英] Beautiful way to remove GET-variables with PHP?
问题描述
我有一个包含GET变量的完整URL的字符串。哪个是删除GET变量的最佳方法?有没有一种很好的方法来删除它们中的一个?
这是一个可行的代码,但不是很漂亮(我认为):
$ current_url = explode('?',$ current_url);
echo $ current_url [0];
上面的代码只是删除了所有的GET变量。在我的情况下,URL是由CMS生成的,因此我不需要任何有关服务器变量的信息。 删除所有变量,也许最漂亮的是
$ url = strtok($ url,'?');
请参阅 strtok
在这里。
下面),并处理不带'?'的url。
获取一个url + querystring并删除一个变量(不使用正则表达式替换,可能会更快在某些情况下),你可能会这样做:
function removeqsvar($ url,$ varname){
list ($ urlpart,$ qspart)= array_pad(explode('?',$ url),2,'');
parse_str($ qspart,$ qsvars);
@unset($ qsvars [$ varname]);
$ newqs = http_build_query($ qsvars);
返回$ urlpart。 '?'。 $ newqs;
}
正则表达式替换为移除单个var可能如下所示:
函数removeqsvar($ url,$ varname){
return preg_replace('/([?&])'。$ varname '= [^&安培;] +(&安培; | $)/', '$ 1',$网址);
}
继承几种不同方法的时间,确保时间在两次运行之间重置。
<?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);
echoregexp 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爆炸执行时间:。$ 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);
echostrpos 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);
echotok execution time:。$ totaltime。seconds;;
显示
正则表达式执行时间:0.14604902267456秒;爆炸执行时间:0.068033933639526秒; strpos执行时间:0.064775943756104秒;执行时间:0.045819044113159秒;
正则表达式执行时间:0.1408839225769秒;爆炸执行时间:0.06751012802124秒; strpos执行时间:0.064877986907959秒;执行时间:0.047760963439941秒;
正则表达式执行时间:0.14162802696228秒;爆炸执行时间:0.065848112106323秒; strpos执行时间:0.064821004867554秒;执行时间:0.041788101196289秒;
正则表达式执行时间:0.14043688774109秒;爆炸执行时间:0.066350221633911秒; strpos执行时间:0.066242933273315秒; tok执行时间:0.041517972946167秒;
正则表达式执行时间:0.14228296279907秒;爆炸执行时间:0.06665301322937秒; strpos执行时间:0.063700199127197秒;执行时间:0.041836977005005秒;
strtok获胜,并且是迄今为止最小的代码。
I have a string with a full URL including GET variables. Which is the best way to remove the GET variables? Is there a nice way to remove just one of them?
This is a code that works but is not very beautiful (I think):
$current_url = explode('?', $current_url);
echo $current_url[0];
The code above just removes all the GET variables. The URL is in my case generated from a CMS so I don't need any information about server variables.
Ok, to remove all variables, maybe the prettiest is
$url = strtok($url, '?');
See about strtok
here.
Its the fastest (see below), and handles urls without a '?' properly.
To take a url+querystring and remove just one variable (without using a regex replace, which may be faster in some cases), you might do something like:
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;
}
A regex replace to remove a single var might look like:
function removeqsvar($url, $varname) {
return preg_replace('/([?&])'.$varname.'=[^&]+(&|$)/','$1',$url);
}
Heres the timings of a few different methods, ensuring timing is reset inbetween runs.
<?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; ";
shows
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 wins, and is by far the smallest code.
这篇关于美丽的方式来删除PHP的GET变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!