我有一个包含完整URL的字符串,包括GET变量.哪个是删除GET变量的最佳方法?有没有一种很好的方法可以删除其中一种?
这是一个有效但不是很漂亮的代码(我认为):
$current_url = explode('?', $current_url); echo $current_url[0];
上面的代码只删除所有GET变量.在我的情况下,URL是从CMS生成的,因此我不需要任何有关服务器变量的信息.
好的,要删除所有变量,也许是最漂亮的
$url = strtok($url, '?');
看到strtok
这里.
它是最快的(见下文),并处理网址没有'?' 正常.
要获取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); return $urlpart . '?' . $newqs; }
删除单个var的正则表达式替换可能如下所示:
function removeqsvar($url, $varname) { return preg_replace('/([?&])'.$varname.'=[^&]+(&|$)/','$1',$url); }
下面介绍几种不同方法的时间,确保在运行之间重置时间.
节目
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获胜,是迄今为止最小的代码.
为'strtok` +1,没有其他人想到:)
2> Gumbo..:怎么样:
preg_replace('/\\?.*/', '', $str)
使用`/(\\?|&)-var =.*?(&| $)/`只删除一个特定的变量(这里是`-var`).
3> Matt Bridges..:如果您尝试从中删除查询字符串的URL是PHP脚本的当前URL,则可以使用前面提到的方法之一.如果您只有一个带有URL的字符串变量,并且您想要去除"?"之外的所有内容.你可以做:
$pos = strpos($url, "?"); $url = substr($url, 0, $pos);
您应该考虑URL可能不包含`?`.然后,您的代码将返回一个空字符串.
4> Scharrels..:受@MitMaro评论的启发,我写了一个小基准来测试@Gumbo,@ Matt Bridges和@justin解决方案的速度问题:
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 "
"; }strtok: 0.12;explode: 0.19;regexp: 0.31;strpos: 0.18; strtok: 0.12;explode: 0.19;regexp: 0.31;strpos: 0.18; strtok: 0.12;explode: 0.19;regexp: 0.31;strpos: 0.18; strtok: 0.12;explode: 0.19;regexp: 0.31;strpos: 0.18; strtok: 0.12;explode: 0.19;regexp: 0.31;strpos: 0.18; strtok: 0.12;explode: 0.19;regexp: 0.31;strpos: 0.18; strtok: 0.12;explode: 0.19;regexp: 0.31;strpos: 0.18; strtok: 0.12;explode: 0.19;regexp: 0.31;strpos: 0.18; strtok: 0.12;explode: 0.19;regexp: 0.31;strpos: 0.18; strtok: 0.12;explode: 0.19;regexp: 0.31;strpos: 0.18;结果:@ justin的strtok是最快的.
注意:使用Apache2和PHP5在本地Debian Lenny系统上进行测试.
5> COil..:另一个解决方案......我发现这个功能更优雅,它还会删除尾随的'?' 如果要删除的密钥是查询字符串中唯一的密钥.
/** * 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; }测试:
$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. '
'; echo removeQueryStringParameter($url, 'test'). '
'; }将输出:
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»在3v4l上运行这些测试