首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用PHP删除GET变量的好方法?

用PHP删除GET变量的好方法?
EN

Stack Overflow用户
提问于 2009-08-09 15:30:17
回答 11查看 166.6K关注 0票数 100

我有一个包含GET变量的完整URL的字符串。删除GET变量的最佳方法是什么?有没有一种很好的方法来删除其中的一个?

这是一个可以工作的代码,但不是很漂亮(我认为):

代码语言:javascript
复制
$current_url = explode('?', $current_url);
echo $current_url[0];

上面的代码只是删除了所有的GET变量。在我的例子中,URL是从CMS生成的,所以我不需要任何关于服务器变量的信息。

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2009-08-09 16:05:05

好的,要删除所有变量,也许最漂亮的是

代码语言:javascript
复制
$url = strtok($url, '?');

请参阅关于strtok here

它是最快的(见下文),并且处理urls时不带'?‘恰如其分。

要获取url+querystring并只删除一个变量(不使用正则表达式替换,这在某些情况下可能会更快),您可以执行如下操作:

代码语言:javascript
复制
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;
}

删除单个var的正则表达式替换可能如下所示:

代码语言:javascript
复制
function removeqsvar($url, $varname) {
    return preg_replace('/([?&])'.$varname.'=[^&]+(&|$)/','$1',$url);
}

下面是几种不同方法的计时,确保在两次运行之间重置计时。

代码语言:javascript
复制
<?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; ";

显示

代码语言:javascript
复制
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胜出,是目前为止最小的代码。

票数 255
EN

Stack Overflow用户

发布于 2009-08-09 15:37:26

这样如何:

代码语言:javascript
复制
preg_replace('/\\?.*/', '', $str)
票数 35
EN

Stack Overflow用户

发布于 2009-08-09 15:40:56

如果您尝试从中删除查询字符串的URL是PHP脚本的当前URL,则可以使用前面提到的方法之一。如果您只有一个包含URL的字符串变量,并且您想要去掉'?‘之后的所有内容您可以执行以下操作:

代码语言:javascript
复制
$pos = strpos($url, "?");
$url = substr($url, 0, $pos);
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1251582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档