Custom Parse_url inverse function Http_build_url, converting an array to a URL string

Source: Internet
Author: User

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

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.