Php uses http_build_query, parse_url, and parse_str to create and parse URLs.

Source: Internet
Author: User
Php uses http_build_query, parse_url, and parse_str to create and parse URLs.

Php uses http_build_query, parse_url, and parse_str to create and parse URLs. if you need them, refer.


1. http_build_query

Http_build_query can be used to create a request string after urlencode.

  1. String http_build_query (mixed $ query_data [, string $ numeric_prefix [, string $ arg_separator [, int $ enc_type = PHP_QUERY_RFC1738])



Parameters:
Query_data
It can be an array or an object containing attributes.

A query_data array can be a simple one-dimensional structure or an array composed of arrays (which can contain other arrays in turn ).

If query_data is an object, only the public attribute is added to the result.

Numeric_prefix
If the numeric subscript is used in the base array and the parameter is given, the value of this parameter is used as the prefix of the numeric subscript element in the base array.

This is to allow PHP or other CGI programs to obtain valid variable names when decoding data later.

Arg_separator
Unless this parameter is specified and used, arg_separator.output is used to separate parameters (this parameter is included in php. ini, and the default value is "&").

Enc_type
PHP_QUERY_RFC1738 is used by default.

If enc_type is PHP_QUERY_RFC1738, the encoding will start? The RFC 1738 standard is used to encode the application/x-www-form-urlencoded media type. spaces are encoded as plus signs (+ ).

If enc_type is PHP_QUERY_RFC3986? RFC 3986 encoding. spaces are percent encoded (% 20 ).


Example 1: Use only the query_data parameter

  1. $ Data = array (
  2. 'Name' => 'fdipzone ',
  3. 'Gender' => 'male ',
  4. 'Comprehension' => 'grammer ',
  5. 'Explain '=> 'A new programmer'
  6. );
  7. Echo http_build_query ($ data );
  8. ?>

Output:
Name = fdipzone & gender = male & compression Sion = programmer & explain = a + new + programmer



Example 2: query_data uses a one-dimensional subscript array to specify
Numeric_prefix = info _, arg_separator = #, enc_type = PHP_QUERY_RFC3986

  1. $ Data = array ('fdipzone ', 'male', 'grammer', 'A new programmer ');
  2. Echo http_build_query ($ data, 'info _ ',' # ', PHP_QUERY_RFC3986 );
  3. ?>

Output:

  1. Info_0 = fdipzone # info_1 = male # info_2 = programmer # info_3 = a % 20new % 20 programmer

2. parse_url

Parse_url parses the url and returns its components

  1. Mixed parse_url (string $ url [, int $ component =-1])



Parameters:
Url
The url to be parsed. invalid characters will be replaced _.

Component
One of PHP_URL_PATH, PHP_URL_QUERY, or PHP_URL_FRAGMENT is used to obtain the string of the specified part of the URL. (Besides PHP_URL_PORT, an integer value is returned ).

Return value:
For URLs that are seriously unqualified, parse_url () may return FALSE.

The returned data generally includes the following types:
Scheme (such as http), host, port, user, pass, path, query (in question mark? And fragment (after the hash symbol)


Example:

  1. $ Url = 'http: // fdipzone: 123456@www.fdipzone.com: 80/test/index. php? Id = 1 # tag ';
  2. Print_r (parse_url ($ url ));
  3. Echo parse_url ($ url, PHP_URL_SCHEME). PHP_EOL;
  4. Echo parse_url ($ url, PHP_URL_HOST). PHP_EOL;
  5. Echo parse_url ($ url, PHP_URL_PORT). PHP_EOL;
  6. Echo parse_url ($ url, PHP_URL_USER). PHP_EOL;
  7. Echo parse_url ($ url, PHP_URL_PASS). PHP_EOL;
  8. Echo parse_url ($ url, PHP_URL_PATH). PHP_EOL;
  9. Echo parse_url ($ url, PHP_URL_QUERY). PHP_EOL;
  10. Echo parse_url ($ url, PHP_URL_FRAGMENT). PHP_EOL;
  11. ?>

Output:

  1. Array
  2. (
  3. [Scheme] => http
  4. [Host] => www.fdipzone.com
  5. [Port] => 80
  6. [User] => fdipzone
  7. [Pass] = & gt; 123456
  8. [Path] =>/test/index. php
  9. [Query] => id = 1
  10. [Fragment] => tag
  11. )
  12. Http
  13. Www.fdipzone.com
  14. 80
  15. Fdipzone
  16. 123456
  17. /Test/index. php
  18. Id = 1
  19. Tag

3. parse_str

Parse_str parses the string into multiple variables

  1. Void parse_str (string $ str [, array & $ arr])

If str is the query string passed by the URL, it is parsed as a variable and set to the current scope.


Parameters:
Str
Input string

Arr
If the second arr variable is set, the variable is saved to the array as an array element.


Example 1: Resolve to the current scope

  1. $ Str = 'name = fdipzone & gender = male & compression Sion = programer & explain = a new programmer ';
  2. Parse_str ($ str );
  3. Echo $ name. PHP_EOL;
  4. Echo $ gender. PHP_EOL;
  5. Echo $ regression Sion. PHP_EOL;
  6. Echo $ explain. PHP_EOL;
  7. ?>

Output:

  1. Fdipzone
  2. Male
  3. Programer
  4. A new programmer



Example 2: save the result to the arr array

  1. $ Str = 'name = fdipzone & gender = male & compression Sion = programer & explain = a new programmer ';
  2. Parse_str ($ str, $ arr );
  3. Print_r ($ arr );
  4. ?>

Output:

  1. Array
  2. (
  3. [Name] => fdipzone
  4. [Gender] => male
  5. [Syntax Sion] => programer
  6. [Explain] => a new programmer
  7. )

4. obtain and parse the url query parameters

First, use parse_url to obtain the query, and then parse the parameters using parse_str.

  1. $ Url = 'http: // www.fdipzone.com/test/index.php? Name = fdipzone & gender = male & compression Sion = programmer & explain = a new programmer ';
  2. $ Query = parse_url ($ url, PHP_URL_QUERY );
  3. Parse_str ($ query, $ data );
  4. Print_r ($ data );
  5. ?>

Output:

  1. Array
  2. (
  3. [Name] => fdipzone
  4. [Gender] => male
  5. [Projection] => programmer
  6. [Explain] => a new programmer
  7. )

Php, http

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.