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.
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];
///