search
Homephp教程php手册php获取当前网址url并替换参数或网址的方法

今天在项目中用到的两个函数,自己写了下,备用,需要的朋友也可以直接使用了,推荐第一个函数为获取,后面为相关的处理参数。

一是PHP获取当前页面的网址:
代码如下:
//获得当前的脚本网址
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.sina.com/a.php?key=330′;
或$url='www.sina.com/a.php;
或$url='www.sina.com/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);
}

不过我的替换是这样写的。。当然也很烂 。。
代码如下:
/**
*使用例程:可用于分页类或页面中的替换等
$url = "add_jd.php?pid=4&tb=gm_jd&page=1";
echo( "原始的URL:" . $url );
echo( '
' );
echo( "字符串参数:" . url::replace( $url , "pid=10,page=2") );
echo( '
' );
echo( "数组型参数:" . url::replace( $url , array('pid'=>10,'page'=>5)) );
//echo( urlReplace( $url , array('pid'=>10,'page'=>5)) );
*/
/**
* url replace
* @param string $url 需要替换的URL字符串,一般为aaa.php?abc=def,也可以带上路径,象http://xxx.com/abc/def.php?aa=bb
* @param mixed $options 需要替换的变量,可以是字符串或数组,如果是字符串,格式为"aa=bb,cc=dd",有多个,用","隔开
* @return string $url 替换后的URL
*/
class url
{
static function replace ( $url , $options)
{
$options = self::optInit( $options );
$Query = parse_url( $url , PHP_URL_QUERY );
if($Query){
parse_str( $Query , $GET );
if ( $GET ){
//foreach ( $GET as $_k => $_v ){
// //if( array_key_exists( $_k , $options)){
// $GET[$_k] = $options[$_k];
// /
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool