The Parse_url function can parse a URL string into an array, but there seems to be no parse_url inverse function in PHP for a parsed array-group composite URL string, with only one http_build_query to synthesize the array group into query String So I wrote myself a http_build_url function to synthesize the complete URL of an array group Parse_url parsed.
/** * Generates a complete URL based on an array of parse_url format * @param array $arr Accept all parameters parsed by Parse_url, the complete parameters are as follows: * Array * (* [Scheme] + HTTP//protocol * [Host] = www.baidu.com//host * [Port] + 80 Port, Optional * [path] +/path/file.php//path (file name), optional * [query] = A=AAA&B=AAABBB Parameter (query string), optional * [fragment] + 123//Additional part or anchor Point (#后面的), optional *)*/functionHttp_build_url ($url _arr){ $new _url=$url _arr[' Scheme ']. "://".$url _arr[' Host ']; if(!Empty($url _arr[' Port '])) $new _url=$new _url.":".$url _arr[' Port ']; $new _url=$new _url.$url _arr[' Path ']; if(!Empty($url _arr[' Query '])) $new _url=$new _url. "?" .$url _arr[' Query ']; if(!Empty($url _arr[' Fragment '])) $new _url=$new _url. "#" .$url _arr[' Fragment ']; return $new _url;}
To use the test:
$url = "http://www.dragondean.cn:80/path/file.php?a=aaa&b=bcdefg#123"; $url _arr Parse_url ($url); Print_r ($url _arr); $url _new = Http_build_url ($url _arr); Echo $url _new;
Operation Result:
Array ( = = http= Www.dragondean. CN = + =/path/file. php = a=aaa&b=bcdefg = 123) http:// Www.dragondean.cn:80/path/file.php?a=aaa&b=bcdefg#123
Custom Parse_url inverse function Http_build_url, converting an array to a URL string