Php retrieves the current url and replaces the parameter or url
Source: Internet
Author: User
The two functions used in the project today are self-written and Standby. you can also directly use them as needed. we recommend that you use the first function for obtaining, followed by related processing parameters. First, PHP retrieves the URL of the current page:
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 .sina.com/a.php? Key = 330 ′;
Or $ url = 'www .sina.com/a.php;
Or $ url = 'www .sina.com/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 ..
The code is as follows:
/**
* Routine: used for paging or page replacement.
$ Url = "add_jd.php? Pid = 4 & tb = gm_jd & page = 1 ";
Echo ("Original URL:". $ url );
Echo ('
');
Echo ("string parameter:". url: replace ($ url, "pid = 10, page = 2 "));
Echo ('
');
Echo ("array parameters:". url: replace ($ url, array ('pid '=> 10, 'page' => 5 )));
// Echo (urlReplace ($ url, array ('pid '=> 10, 'page' => 5 )));
*/
/**
* Url replace
* @ Param string $ the url string to be replaced, which is generally aaa. php? Abc = def, can also carry a path, like a http://xxx.com/abc/def.php? Aa = bb
* @ Param mixed $ options: the variable to be replaced. it can be a string or an array. if it is a string, the format is "aa = bb, cc = dd". there are multiple variables ", "Separated
* @ Return string $ the url after the URL is replaced
*/
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];
///
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.