php to get URL and replace the parameter or URL method

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags .url array automatic code echo example function get

The code is as follows:
Get 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 the PHP replacement URL in the query section of a variable value for example, we want to set the $url in the key=321;
In fact, there are several situations:
$url = ' www.jzread.com/a.php tutorial? key=330′;
or $url= ' www.jzread.com/a.php;
or $url= ' www.jzread.com/a.php?cat=2′;
Wait a minute. Although the situation is many, PHP is very simple to deal with, as follows:
Copy code code as follows:
/* Set a parameter in the URL to a value//"This is a good paragraph."
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);
}

But my replacement was written like this. Of course it sucks.

Copy code code as follows:
<?php
/**
* Use routines: can be used for pagination classes or replacements on pages, and so on
$url = "add_jd.php?pid=4&tb=gm_jd&page=1";
Echo (original URL:. $url);
Echo (' <br/> ');
Echo (string parameter:. Url::replace ($url, "pid=10,page=2"));
Echo (' <br/> ');
Echo (array parameter:. Url::replace ($url, Array (' PID ' =>10, ' page ' =>5));
//echo (Urlreplace ($url, Array (' PID ' =>10, ' page ' =>5));
/
/**
* URL Replace
* @param string $url a URL string that needs to be replaced, typically aaa.php?abc=def, or with a path, like http://xxx.com/abc/ DEF.PHP?AA=BB
* @param mixed $options The variable that needs to be replaced, can be a string or an array, if it is a string, the format is "AA=BB,CC=DD" and there are multiple, separated by ","
* @return String $ URL replaced URL
*/
Class URL
{
static function replace ($url, $options)
{
$options = Self::optinit ($opt ions);
$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];
////}
//}
$GET = Array_merge ($GET, $options);
}
Return Str_replace ($Query, Http_build_query ($GET), $url);
}
if (! $Query && $options) {
return $url. "?" . Http_build_query ($options);
}
return $url;
}
Static Private function Optinit ($options)
{
if (is_string ($options)) {
$optlists = power::normalize ($options);
foreach ($optlists as $val) {
List ($tmpKey, $tmpVal) = Power::normalize ($val, "=");
$opts [$tmpKey] = $tmpVal;
}
}else{
$opts = $options;
}
//unset ($options);
return $opts;
}

Although something is considered, it is only a general solution.

The following are some additional information:
Example: I need to get the current URL address
$url _this = "http://" $_server [' http_host '].$_server[' php_self '];
echo $url _this;

Shows: http://localhost/lu.php

Server variables: $_server
Note: Used in PHP 4.1.0 and later versions. Previous versions, using $HTTP _server_vars.

$_server is an array that contains such things as the head (headers), the path (paths), and the script location (scripts locations). The entities of the array are created by the Web server. There is no guarantee that all servers will generate all the information, the server may have overlooked some information, or some new information that has not been listed below. This means that a large number of these variables are described in the CGI 1.1 specification, so you should study it carefully.

This is a "superglobal" or can be described as an automatic global variable. This simply means that it works in all scripts. You do not need to use global $_server in a function or method; Access it just as you would with $HTTP _server_vars.

$HTTP _server_vars contains the same information, but is not an automatic global variable. (Note: $HTTP _server_vars and $_server are different variables, PHP handles them in different ways.) )

If the register_globals directive is set, these variables are also available in all scripts, that is, the $_server and $HTTP _server_vars arrays are separated. For information, please refer to the Security section for the use of the Register Globals. These individual global variables are not automatic global variables.

You may find that some of the $_server elements listed below are not available. Note that if you run PHP on a command-line basis, the elements listed below are almost not valid (or meaningless).


"Php_self"
The name of the file currently executing the script, related to document root. For example, using $_server[' php_self ' in a script with a URL address of Http://example.com/test.php/foo.bar will get/test.php/foo.bar this result.

If PHP runs as a command-line, the variable is not valid.

Related Article

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.