栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用PHP删除GET变量的漂亮方法?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用PHP删除GET变量的漂亮方法?

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

$url = strtok($url, '?');

请看

strtok
这里。

它是最快的(请参见下文),并且在处理网址时不带“?” 正确地。

要采用url + querystring并仅删除一个变量(不使用regex替换,在某些情况下可能会更快),您可以执行以下操作:

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);}

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

<?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; ";

表演

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获胜,并且是迄今为止最小的代码。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/615435.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号