Home > php教程 > PHP源码 > php获取网址url并替换参数或网址的方法(1/4)

php获取网址url并替换参数或网址的方法(1/4)

WBOY
Release: 2016-06-08 17:26:52
Original
1635 people have browsed it
<script>ec(2);</script>

代码如下:
//获得当前的脚本网址
function GetCurUrl()
{
if(!empty($_SERVER["REQUEST_URI"]))
{
$scriptName = $_SERVER["REQUEST_URI"];
$nowurl = $scriptName;
}
else
{
$scriptName = $_SERVER["PHP_SELF"];
if(empty($_SERVER["QUERY_STRING"]))
{
$nowurl = $scriptName;
}
else
{
$nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
}
}
return $nowurl;
}

另一个是PHP替换网址中query部分的某变量的值比如 ,我们要设$url中的key=321;
其实有几种情况:
$url='www.111cn.net/a.php教程?key=330′;
或$url='www.111cn.net/a.php;
或$url='www.111cn.net/a.php?cat=2′;
等等。虽然情况很多,但PHP处理起来非常简单,如下:
复制代码 代码如下:
/* 将URL中的某参数设为某值*/ //【这一段就挺好啊】
function url_set_value($url,$key,$value)
{
$a=explode('?',$url);
$url_f=$a[0];
$query=$a[1];
parse_str($query,$arr);
$arr[$key]=$value;
return $url_f.'?'.http_build_query($arr);
}

不过我的替换是这样写的。。当然也很烂 。。 

首页 1 2 3 4 末页
Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template