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

编程入门 行业动态 更新时间:2024-10-23 13:26:38
本文介绍了用 PHP 删除 GET 变量的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个包含 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);}

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

<?php$number_of_tests = 40000;$mtime = microtime();$mtime = expand(" ",$mtime);$mtime = $mtime[1] + $mtime[0];$starttime = $mtime;for($i = 0; $i < $number_of_tests; $i++){$str = "www.example?test=test";preg_replace('/\?.*/', '', $str);}$mtime = microtime();$mtime = expand(" ",$mtime);$mtime = $mtime[1] + $mtime[0];$endtime = $mtime;$totaltime = ($endtime - $starttime);echo "regexp 执行时间: ".$totaltime." seconds; ";$mtime = microtime();$mtime = expand(" ",$mtime);$mtime = $mtime[1] + $mtime[0];$starttime = $mtime;for($i = 0; $i < $number_of_tests; $i++){$str = "www.example?test=test";$str =explode('?', $str);}$mtime = microtime();$mtime = expand(" ",$mtime);$mtime = $mtime[1] + $mtime[0];$endtime = $mtime;$totaltime = ($endtime - $starttime);echo "explode 执行时间: ".$totaltime." seconds; ";$mtime = microtime();$mtime = expand(" ",$mtime);$mtime = $mtime[1] + $mtime[0];$starttime = $mtime;for($i = 0; $i < $number_of_tests; $i++){$str = "www.example?test=test";$qPos = strpos($str, "?");$url_without_query_string = substr($str, 0, $qPos);}$mtime = microtime();$mtime = expand(" ",$mtime);$mtime = $mtime[1] + $mtime[0];$endtime = $mtime;$totaltime = ($endtime - $starttime);echo "strpos 执行时间: ".$totaltime." seconds; ";$mtime = microtime();$mtime = expand(" ",$mtime);$mtime = $mtime[1] + $mtime[0];$starttime = $mtime;for($i = 0; $i < $number_of_tests; $i++){$str = "www.example?test=test";$url_without_query_string = strtok($str, '?');}$mtime = microtime();$mtime = expand(" ",$mtime);$mtime = $mtime[1] + $mtime[0];$endtime = $mtime;$totaltime = ($endtime - $starttime);echo "tok 执行时间: ".$totaltime." seconds; ";

节目

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 = "www.example?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 = "www.example?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 = "www.example?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 = "www.example?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 变量的好方法?

本文发布于:2023-11-01 18:27:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550077.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   方法   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!