PHP的url参数获取问题,如何快速获取所需参数

WBOY
Release: 2016-06-23 13:26:50
Original
924 people have browsed it

http://www.abc.com/content/5176.asp?page=1&5176__2#p

当前url是这个链接
我现在需要在当前页面上生成1个链接
http://www.abc.com/content/5176.asp?page=1&author=10000

如何快速完成??
我的做法是:

function ghref($param){	$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; //获取url	$query_str = parse_url($url, PHP_URL_QUERY); //获取url问号后的值,不包括#	$query_str =explode("&",$query_str); //切割成数组	$arr=array();     foreach($query_str  as $v){           	if(preg_match("/=/",$v)){        		$arr[] = $v;  //标准的键名=参数值,就留下        	}     }     $str = implode("&",$arr); //变成url字符串	parse_str($str,$myArray); //再次分成数组形式	$myArray['page']=$param; //修改其中某个键名的值       $myArray['author']=1000;	return substr($url,0,strpos($url,"?"))."?".http_build_query($myArray); //完整拼接}
Copy after login


我觉得有点繁琐了,哪路大神有简便的做法???


回复讨论(解决方案)

你的 asp 也用 php 解析?
对于 http://www.abc.com/content/5176.asp?page=1&5176__2#p
$_GET 是这样的数组 array('page' => 1, '5176__2' => ''),#p 不会被 php 利用
于是你可以 $_GET = array_diff($_GET, array('')); 得到这样的数组 array('page' => 1)
$_GET['author'] = 1000;
return ""http://$_SERVER[HTTP_HOST]/$_SERVER[PHP_SELF]?" . http_build_query($_GET);

膜拜xuzuning大神,非常感谢帮助

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template