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)