PHP parses the URL and obtains the parameters in the URL.

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. Go to PHP to parse the URL and get the parameters in the URL? Php illustrates a URL-formatted string: $ str1 uses parse_url to parse the URL, which is $ str $ arrparse_url ($ str); var_dump ($ arr ); 2. Extract the parameters in the URL and put them in the array $ arr _

Welcome to the Linux community forum and interact with 2 million technical staff> go to PHP to parse the URL and get the parameters in the URL? Php // example of a URL string: $ str // 1 use parse_url to parse the URL, where $ str $ arr = parse_url ($ str); var_dump ($ arr ); // 2 obtain the URL parameters and put them in the array $ arr _

Welcome to the Linux community forum and interact with 2 million technicians>

PHP parses the URL and obtains the parameters in the URL.

  

// Example of a URL string:

$ Str

// 1 use parse_url to parse the URL, which is $ str

$ Arr = parse_url ($ str );

Var_dump ($ arr );

// 2. Extract the parameters in the URL and put them in the array.

$ Arr_query = convertUrlQuery ($ arr ['query']);

Var_dump ($ arr_query );

// 3 change the parameter array back to the string format

Var_dump (getUrlQuery ($ arr_query ));

/**

* Returns the url query as associative array

* @ Param string query

* @ Return array params

*/

Function convertUrlQuery ($ query)

{

$ QueryParts = explode ('&', $ query );

$ Params = array ();

Foreach ($ queryParts as $ param)

{

$ Item = explode ('=', $ param );

$ Params [$ item [0] = $ item [1];

}

Return $ params;

}

Function getUrlQuery ($ array_query)

{

$ Tmp = array ();

Foreach ($ array_query as $ k => $ param)

{

$ Tmp [] = $ k. '='. $ param;

}

$ Params = implode ('&', $ tmp );

Return $ params;

}

?>

Output result:

Array (size = 4)

'Scheme '=> string 'HTTP' (length = 4)

'Host' => string 'test. com' (length = 8)

'Path' => string '/testdir/index. php' (length = 18)

'Query' => string 'param1 = 10 & param2 = 20 & param3 = 30 & param4 = 40 & param5 = 50 & param6 = 60' (length = 59)

Array (size = 6)

'Param1' => string '10' (length = 2)

'Param2' => string '20' (length = 2)

'Param3' => string '30' (length = 2)

'Param4' => string '40' (length = 2)

'Param5' => string '50' (length = 2)

'Param6' => string '60' (length = 2)

String 'param1 = 10 & param2 = 20 & param3 = 30 & param4 = 40 & param5 = 50 & param6 = 60' (length = 59)

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.