Summary of how php obtains url parameters. php obtains url parameters.

Source: Internet
Author: User

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.

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.