Home >Backend Development >PHP Tutorial >PHP的url参数获取有关问题,怎么快速获取所需参数

PHP的url参数获取有关问题,怎么快速获取所需参数

WBOY
WBOYOriginal
2016-06-13 12:24:25880browse

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


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

------解决思路----------------------
你的 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);

Statement:
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