PHP gets the parameters for each part of the URL

Source: Internet
Author: User

URL handles several key functions Parse_url, PARSE_STR, and Http_build_query

Parse_url ()

The function can parse the URL and return its constituent parts. Its usage is as follows:

Array Parse_url (string $url)

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-such as localhost
    • Port-If 80
    • User
    • Pass
    • Path-such as/parse_str.php
    • Query-in question marks? Then like Id=1&category=php&title=php-install
    • Fragment-After hash symbol #

This function does not imply that the given URL is legal, it simply separates the parts of the list above. Parse_url () can accept an incomplete URL and try to parse it correctly. This function has no effect on the URL of the relative path.

123456 <?php    $url= "http://52php.cnblogs.com/welcome/";    $parts = parse_url($url);        print_r($parts);?>

The results of the program run as follows:

123456 Array (     [scheme] => http     [host] => 52php.cnblogs.com     [path] => /welcome/ )
123456 <?php    $url = ‘http://username:[email protected]/path?arg=value#anchor‘;    print_r(parse_url($url));    echo ‘<br />‘;    echo parse_url($url, PHP_URL_PATH);?>

Program output:

12345678910 Array (    [scheme] => http    [host] => hostname    [user] => username    [pass] => password    [path] => /path    [query] => arg=value    [fragment] => anchor)

As you can see, it's easy to break out the various parts of a URL, which is easy if you want to take the specified part out, such as:

1 echoparse_url($url, PHP_URL_PATH);

In the second parameter, set the following parameters: 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.

Parse_str ()

PARSE_STR is used to parse the query string in the URL, which is the string value that can be obtained by $_server[' query_string ') if the URL we requested is http://localhost/parse_str.php?id= 1&category=php&title=php-install, then $_server[' query_string ') returns a value of id=1&category=php&title= Php-install, and this form of string happens to be parsed into an associative array using PARSE_STR.

Use the following:

void Parse_str (String $str [, Array & $arr])

The function receives two parameters, $str the string to parse, and the optional argument $arr the variable name that is stored for the array value generated after parsing, and if the optional argument is omitted, a variable that resembles $id, $category, $title can be called directly. The following script simulates a GET request.

12345678910111213141516171819202122 <?php<a href="http://localhost/parse_str.php?id=1&category=php&title=php-install">Click Here</a>$query_str = $_SERVER[‘QUERY_STRING‘];parse_str($query_str); /* 这种方式可以直接使用变量$id, $category, $title */parse_str($query_str, $query_arr);?><pre><?php print_r($query_arr); ?></pre><p><?php echo $id; ?></p><p><?php echo $category; ?></p><p><?php echo $title; ?></p>?>/* 运行结果 */Array(    [id] => 1    [category] => php    [title] => php-install)1phpphp-install

Http_build_query is the conversion of an array into a URL, followed by a string of arguments that are automatically UrlEncode processed

String Http_build_query (Array formdata [, String Numeric_prefix])

Add subscript to the array without specifying a key or a key as a number

Official manual: Http://php.net/manual/zh/function.http-build-query.php

PHP gets the parameters for each part of the URL

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.