Php method for obtaining url and replacing parameters or url (1/4)-PHP source code

Source: Internet
Author: User
Tags php website
Ec (2); the Code is as follows: Get the current Script URL functionGetCurUrl () {if (! Empty ($ _ SERVER [& quot; REQUEST_URI & quot;]) {$ scriptName $ _ SERVER [& quot; REQUEST_URI & quot;]; $ nowurl $ scriptName ;} else {$ scriptNa script ec (2); script

The Code is as follows:
// Obtain the current Script URL
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;
}

The other is to replace the value of a variable in the query part of the PHP website. For example, we need to set key = 321 in $ url;
In fact, there are several situations:
$ Url = 'www .111cn.net/a.php tutorial? Key = 330 ′;
Or $ url = 'www .111cn.net/a.php;
Or $ url = 'www .111cn.net/a.php? Cat = 2 ′;
And so on. Although there are many situations, PHP is very simple to handle, as shown below:
The Code is as follows:
/* Set a parameter in the URL to a value * // [This paragraph is quite good]
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 );
}

However, my replacement is written in this way .. Of course ..

Homepage 1 2 3 4 last

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.