PHP Parse_url () function and parse_str () function

Source: Internet
Author: User

1.parse_url (): Parses the URL and returns its constituent parts

Prototype: Mixed Parse_url (string $url [, int $component =-1])

This function returns an associative array that contains the various components of an existing URL. If one of these is missing, an array entry is not created for this component. The components are:

    • Scheme-such as HTTP
    • Host
    • Port
    • User
    • Pass
    • Path
    • Query-in question marks? After
    • Fragment-After hash symbol #
?PHP$url= ' Http://username:[email protected]:9090/path?arg=value#anchor '; Var_dump(Parse_url($url)); Var_dump(Parse_url($url,php_url_scheme)); Var_dump(Parse_url($url,php_url_user)); Var_dump(Parse_url($url,php_url_pass)); Var_dump(Parse_url($url,php_url_host)); Var_dump(Parse_url($url,php_url_port)); Var_dump(Parse_url($url,Php_url_path)); Var_dump(Parse_url($url,php_url_query)); Var_dump(Parse_url($url,php_url_fragment));?>
Result: Array (8) {["scheme"] + string (4) "http" ["Host"] + string (8) "hostname" ["port"] = = T (9090) ["user"] + = string (8) "username" ["Pass"] + string (8) "Password" ["Path"] + string (5) "/path" ["query"] = + string (9) "Arg=value" ["Fragment"] + string (6) "anchor"}

If there is a second parameter set, Php_url_scheme, Php_url_host, Php_url_port, Php_url_user, Php_url_pass, Php_url_path, php_url_query or PHP_ Url_fragment. You can remove a specific section.

2.parse_str (): Parses the query string into a variable.

Prototype: void Parse_str (String $encoded _string [, Array & $result])

<?PHP$str= "First=value&arr[]=foo+bar&arr[]=baz"; //Recommended    Parse_str($str,$output); Echo $output[' first '];//value    Echo $output[' arr '] [0];//Foo Bar    Echo $output[' arr '] [1];//Baz//discouraged    Parse_str($str); Echo $first;//value    Echo $arr[0];//Foo Bar    Echo $arr[1];//Baz?>

PHP Parse_url () function and parse_str () function

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.