This article mainly introduces the php method for obtaining url parameters. The example summarizes the method for parsing URL using the parse_url () function. For more information, see the example in this article. Share it with you for your reference. The details are as follows:
There are many methods to obtain url parameters in php. The simplest method is to directly use the parse_url function, it can easily and quickly automatically parse url parameters and values and store them in the corresponding array. Other methods are basically operated by regular expressions.
Parse_url function
Let's take a look at the parse_url function.
Note:
Mixed parse_url (string $ url [, int $ component =-1])
This function parses a URL and returns an associated array, which contains various components in the URL.
This function is not used to verify the validity of a given URL, but is used to separate it into the parts listed below. Incomplete URLs are also accepted, and parse_url () tries to parse them correctly as much as possible.
The URL to be parsed. Invalid characters are replaced.
Example:
The code is as follows:
$ Url = "http://www.bitsCN.com/welcome ";
$ Parts = parse_url ($ url );
Print_r ($ parts );
Array
(
[Scheme] => http
[Host] => www.bitsCN.com
[Path] =>/welcome/
)
You can also write an algorithm on your own! As follows:
The code is as follows:
Function getParams ()
{
$ Url = '/index. php? _ P = index & _ a = show & x = 12 & y = 23 ';
$ Refer_url = parse_url ($ url );
$ Params = $ refer_url ['query'];
$ Arr = array ();
If (! Empty ($ params ))
{
$ ParamsArr = explode ('&', $ params );
Foreach ($ paramsArr as $ k => $ v)
{
$ A = explode ('=', $ v );
$ Arr [$ a [0] = $ a [1];
}
}
Return $ arr;
}
Call method
The code is as follows:
$ Arr = getParams ();
Print_r ($ arr );
The running result is as follows:
The code is as follows:
Array ([_ p] => index [_ a] => show [x] => 12 [y] => 23)
I hope this article will help you with PHP programming.