How to quickly get the parameter name and the parameter value in the URL address (when looking at the PHP manual, inadvertently see these two functions, guess can be used together.) )

Source: Internet
Author: User

Today, when we look at the manual, I inadvertently see the two built-in functions Parse_url and PARSE_STR. After seeing their usage, I suddenly thought that I could use these two functions to get the parameters in the URL address. To verify my guess, I'll take the following URL as an example: https://www.baidu.com/baidu?wd= Baidu &tn=monline_4_dg&ie=utf-8

The code is as follows:

$url = ' https://www.baidu.com/baidu?wd= Baidu &tn=monline_4_dg&ie=utf-8 '; $url _info Parse_url ($url); // Print_r ($arr); Parse_str ($url _info[' query '],$query); Print_r ($query);

Here we first print out the information of the $url_info array, such as:

We see a return of four messages: protocol header, domain name, path, parameters. Here we then use the PARSE_STR function to manipulate the parameters.

Print out the array information after the PARSE_STR function is processed such as:

So we get a one-dimensional associative array with the parameter name key and the value of the parameter as the value. Also proves that my conjecture is correct.

Through this I learned the following PHP built-in functions:

Mixed parse_url ( string $url [, int $component =-1])

resolves the URL, returning its components . The return value is a one-dimensional associative array that returns false on error.

The first parameter is the URL address that is parsed, and the second parameter returns the array if it is not, and returns the specified value if it is established.

For example :

=============================================================================================================== ========

void parse_str ( string $str [, array &$arr ])

Parses a string into multiple variables. No return value!

The first argument is the parsed string, and the second parameter is the array name (all parsed information is saved to the one-dimensional associative array in the array).

Examples are as follows:

$str= "First=value&arr[]=foo+bar&arr[]=baz";Parse_str($str);Echo $first;//valueEcho $arr[0];//Foo BarEcho $arr[1];//BazParse_str($str,$output);Echo $output[' first '];//valueEcho $output[' arr '] [0];//Foo BarEcho $output[' arr '] [1];//Baz

How to quickly get the parameter name and the parameter value in the URL address (when looking at the PHP manual, inadvertently see these two functions, guess can be used together.) )

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.