Summary of how php obtains url parameters. php obtains url parameters.
This example describes how to obtain url parameters in php. 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:
Copy codeThe Code is as follows: $ url = "http://www.bkjia.com/welcome ";
$ Parts = parse_url ($ url );
Print_r ($ parts );
Array
(
[Scheme] => http
[Host] => www.jb51.net
[Path] =>/welcome/
)
You can also write an algorithm on your own! As follows:
Copy codeThe 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
Copy codeThe Code is as follows: $ arr = getParams ();
Print_r ($ arr );
The running result is as follows:
Copy codeThe Code is as follows: Array ([_ p] => index [_ a] => show [x] => 12 [y] => 23)
I hope this article will help you with PHP programming.