用PHP删除GET变量的漂亮方法?
我有一个包含GET变量的完整URL的字符串。删除GET变量的最佳方法是什么?有没有一种很好的方法可以移除其中的一个 这是一个可以工作但不是很漂亮的代码(我认为): 上面的代码只是删除了所有GET变量。在我的例子中,URL是从CMS生成的,因此我不需要任何关于服务器变量的信息。您可以使用,例如用PHP删除GET变量的漂亮方法?,php,url,variables,get,Php,Url,Variables,Get,我有一个包含GET变量的完整URL的字符串。删除GET变量的最佳方法是什么?有没有一种很好的方法可以移除其中的一个 这是一个可以工作但不是很漂亮的代码(我认为): 上面的代码只是删除了所有GET变量。在我的例子中,URL是从CMS生成的,因此我不需要任何关于服务器变量的信息。您可以使用,例如$\u server['REQUEST\u URI'],或者更好的方法:$\u server['PHP\u SELF']如何: preg_replace('/\\?.*/', '', $str) 如果试图从
$\u server['REQUEST\u URI']
,或者更好的方法:$\u server['PHP\u SELF']
如何:
preg_replace('/\\?.*/', '', $str)
如果试图从中删除查询字符串的URL是PHP脚本的当前URL,则可以使用前面提到的方法之一。如果您只是有一个包含URL的字符串变量,并且您希望去掉“?”之外的所有内容,则可以执行以下操作:
$pos = strpos($url, "?");
$url = substr($url, 0, $pos);
通过循环$\u GET数组重写查询字符串的函数怎么样 !!适当函数的大致轮廓
function query_string_exclude($exclude, $subject = $_GET, $array_prefix=''){
$query_params = array;
foreach($subject as $key=>$var){
if(!in_array($key,$exclude)){
if(is_array($var)){ //recursive call into sub array
$query_params[] = query_string_exclude($exclude, $var, $array_prefix.'['.$key.']');
}else{
$query_params[] = (!empty($array_prefix)?$array_prefix.'['.$key.']':$key).'='.$var;
}
}
}
return implode('&',$query_params);
}
像这样的东西将是很好的保持方便的分页链接等
<a href="?p=3&<?= query_string_exclude(array('p')) ?>" title="Click for page 3">Page 3</a>
受@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 "<br />";
}
functionteststrok($number\u of\u tests){
对于($i=0;$i<$u测试次数;$i++){
$str=”http://www.example.com?test=test";
$str=strtok($str,“?”);
}
}
函数testexplode($测试次数){
对于($i=0;$i<$u测试次数;$i++){
$str=”http://www.example.com?test=test";
$str=爆炸(“?”,$str);
}
}
函数testregexp($测试次数){
对于($i=0;$i<$u测试次数;$i++){
$str=”http://www.example.com?test=test";
预替换('/\?.*/','$str);
}
}
功能测试strpos($测试的数量){
对于($i=0;$i<$u测试次数;$i++){
$str=”http://www.example.com?test=test";
$qPos=STRPO($str,“?”);
$url\u不带\u查询\u字符串=substr($str,0,$qPos);
}
}
$number\u运行次数=10次;
对于($runs=0;$runs<$numberofruns;$runs++){
$测试次数=40000次;
$functions=array(“strtok”、“explode”、“regexp”、“strpos”);
foreach($func的功能){
$starttime=微时间(真);
调用用户函数(“测试”。$func,$tests);
echo$func.:“.sprintf(“%0.2f”),微时间(真)-$starttime.;”;
}
回声“
”;
}
斯特托克:0.12;爆炸:0.19;regexp:0.31;strpos:0.18;
斯特托克:0.12;爆炸:0.19;regexp:0.31;strpos:0.18;
斯特托克:0.12;爆炸:0.19;regexp:0.31;strpos:0.18;
斯特托克:0.12;爆炸:0.19;regexp:0.31;strpos:0.18;
斯特托克:0.12;爆炸:0.19;regexp:0.31;strpos:0.18;
斯特托克:0.12;爆炸:0.19;regexp:0.31;strpos:0.18;
斯特托克:0.12;爆炸:0.19;regexp:0.31;strpos:0.18;
斯特托克:0.12;爆炸:0.19;regexp:0.31;strpos:0.18;
斯特托克:0.12;爆炸:0.19;regexp:0.31;strpos:0.18;
斯特托克:0.12;爆炸:0.19;regexp:0.31;strpos:0.18;
结果:@justin的strtok是最快的
注:使用Apache2和PHP5在本地Debian Lenny系统上进行测试。好的,要删除所有变量,最漂亮的可能是
$url = strtok($url, '?');
看看
它是最快的(见下文),可以正确处理没有“?”的URL
要获取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;
}
用于删除单个变量的正则表达式替换可能如下所示:
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; ";
strtok获胜,并且是迄今为止最小的代码。您不能使用服务器变量来实现这一点吗
@list($url) = explode("?", $url, 2);
或者这行得通吗
unset($_GET['page']);
$url = $_SERVER['SCRIPT_NAME'] ."?".http_build_query($_GET);
只是想一想。basename($\u SERVER['REQUEST\u URI'])
返回“?”之后的所有内容
在我的代码中,有时我只需要部分,所以将它分开,这样我就可以动态地获得所需的值。
与其他方法相比,不确定性能速度,但它确实对我有用
$urlprotocol = 'http'; if ($_SERVER["HTTPS"] == "on") {$urlprotocol .= "s";} $urlprotocol .= "://";
$urldomain = $_SERVER["SERVER_NAME"];
$urluri = $_SERVER['REQUEST_URI'];
$urlvars = basename($urluri);
$urlpath = str_replace($urlvars,"",$urluri);
$urlfull = $urlprotocol . $urldomain . $urlpath . $urlvars;
另一个解决方案。。。我发现这个函数更优雅,如果要删除的键是查询字符串中唯一的键,它还将删除尾部的“?”
/**
* 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. '<br/>';
echo removeQueryStringParameter($url, 'test'). '<br/><br/>';
}
在我看来,最好的办法是:
<? if(isset($_GET['i'])){unset($_GET['i']); header('location:/');} ?>
它会检查是否存在“i”GET参数,如果存在,则会将其删除。只需使用echo的javascript,通过一个自提交的空白表单将URL中的所有变量删除即可:
<?
if (isset($_GET['your_var'])){
//blah blah blah code
echo "<script type='text/javascript'>unsetter();</script>";
?>
这当然假设他正在解析的url就是正在进行解析的页面。绝对更漂亮。我不知道哪一个会表现得更好+这为我节省了几行,对我来说,这又短又漂亮。非常感谢。使用/(\\?&)var=.*(&$)/
只删除一个特定变量(这里的var
),除非性能不是问题,否则我会坚持使用您现有的变量。Gumbo提供的regex解决方案将尽其所能地漂亮。如果它在functions.php中,或者无论你在哪里隐藏你的丑陋之处,你都不需要漂亮,你只需要看到qs_build()来调用它。有一种方法可以通过一个好的匿名函数来实现这一点。url片段怎么样?下面我所看到的解决方案都会像你的代码一样丢弃这个片段。+ 1,因为它是这里唯一回答这个问题并提供另一种选择的答案。你应该考虑URL可能不包含<代码>?<代码>。你的代码将返回一个空字符串。是的,为了支持@Gumbo所说的,我将第二行改为:$url=($pos)?substr($url,0,$pos):$url代码>regexp执行时间:0.14591598510742秒;爆炸执行时间:0.07137393951416秒;strpos执行时间:0.080883026123047秒;tok执行时间:0.042459011077881秒;很不错的!我认为速度很重要。这不是唯一会发生的事情。一个web应用程序可以有数百种功能。“一切都在细节中”。谢谢,投赞成票!贾斯汀,谢谢。脚本现在已清理
$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. '<br/>';
echo removeQueryStringParameter($url, 'test'). '<br/><br/>';
}
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
<? if(isset($_GET['i'])){unset($_GET['i']); header('location:/');} ?>
<?
if (isset($_GET['your_var'])){
//blah blah blah code
echo "<script type='text/javascript'>unsetter();</script>";
?>
function unsetter() {
$('<form id = "unset" name = "unset" METHOD="GET"><input type="submit"></form>').appendTo('body');
$( "#unset" ).submit();
}