删除php变量,用PHP删除GET变量的美丽的方式?

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

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

它是最快的(见下文),并处理没有’?’的网址正确。

要获取一个url querystring和只删除一个变量(不使用regex replace,在某些情况下可能更快),你可能会做:

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

}

掌握几种不同方法的时序,确保在运行之间重置时序。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值