用 PHP 删除 GET 变量的好方法? [英] Beautiful way to remove GET-variables with PHP?
问题描述
我有一个包含 GET 变量的完整 URL 字符串.删除 GET 变量的最佳方法是什么?有什么好方法可以只删除其中一个吗?
这是一个有效但不是很漂亮的代码(我认为):
$current_url = expand('?', $current_url);回声 $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);未设置($qsvars[$varname]);$newqs = http_build_query($qsvars);返回 $urlpart .'?.$newqs;}
用于移除单个 var 的正则表达式替换可能如下所示:
function removeqsvar($url, $varname) {return preg_replace('/([?&])'.$varname.'=[^&]+(&|$)/','$1',$url);}
以下是几种不同方法的计时,确保在两次运行之间重置计时.
节目
regexp 执行时间:0.14604902267456 秒;爆炸执行时间:0.068033933639526秒;strpos 执行时间:0.064775943756104 秒;tok 执行时间:0.045819044113159 秒;正则表达式执行时间:0.1408839225769 秒;爆炸执行时间:0.06751012802124 秒;strpos 执行时间:0.064877986907959 秒;tok 执行时间:0.047760963439941 秒;正则表达式执行时间:0.14162802696228 秒;爆炸执行时间:0.065848112106323秒;strpos 执行时间:0.064821004867554 秒;tok 执行时间:0.041788101196289 秒;正则表达式执行时间:0.14043688774109 秒;爆炸执行时间:0.066350221633911秒;strpos 执行时间:0.066242933273315 秒;tok 执行时间:0.041517972946167 秒;正则表达式执行时间:0.14228296279907 秒;爆炸执行时间:0.06665301322937秒;strpos 执行时间:0.063700199127197 秒;tok 执行时间: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屋!