PHP uses HTTP_BUILD_QUERY,PARSE_URL,PARSE_STR to create and parse URLs

Source: Internet
Author: User
Tags mixed rfc
1.http_build_query

Http_build_query can create a request string after UrlEncode.

String Http_build_query (mixed $query _data [, String $numeric _prefix [, String $arg _separator [, int $enc _type = Php_quer Y_RFC1738]])



Parameters:
Query_data
can be an array or an object that contains attributes.

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

If Query_data is an object, only the public attribute will add the result.

Numeric_prefix
If a numeric subscript is used in the underlying array and the parameter is given, the parameter value is prefixed to the numeric subscript element in the underlying array.

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

Arg_separator
Unless this parameter is specified and used, Arg_separator.output is used to separate the parameters (PHP.ini has this parameter and the default is "&").

Enc_type
php_query_rfc1738 is used by default.

If Enc_type is php_query_rfc1738, the encoding will be encoded in the»RFC 1738 standard and application/x-www-form-urlencoded media type, and the space will be encoded as a plus sign (+).

If the Enc_type is php_query_rfc3986, it will be encoded according to»RFC 3986, and the space will be encoded with a percent sign (%20).


Example 1: use only the Query_data parameter

<?php
$data = Array (
    ' name ' => ' Fdipzone ', '
    gender ' => ' male ',
    ' profession ' => ') Programmer ',
    ' explain ' => ' a new programmer '
);
echo Http_build_query ($data);
? >

Output:
Name=fdipzone&gender=male&profession=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

<?php
$data = Array (' Fdipzone ', ' Male ', ' programmer ', ' a new programmer ');
Echo http_build_query ($data, ' info_ ', ' # ', php_query_rfc3986);
>

Output:

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


2.parse_url

parse_url resolves the URL and returns its components

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



Parameters:
URL
The URL to resolve, invalid characters will be replaced with _

Component
One of the Php_url_path, Php_url_query, or php_url_fragment to get a string of the part specified in the URL. (An integer value is returned, except when specified as Php_url_port.)

return Value:
The severely unqualified url,parse_url () may return FALSE.

The data returned typically includes the following types of
Scheme (such as HTTP), Host,port,user,pass,path,query (after the question mark?), fragment (after the hash symbol #)


Example:

<?php
$url = ' Http://fdipzone:123456@www.fdipzone.com:80/test/index.php?id=1#tag ';
Print_r (Parse_url ($url));

Echo Parse_url ($url, Php_url_scheme). Php_eol;
Echo Parse_url ($url, Php_url_host). Php_eol;
Echo Parse_url ($url, Php_url_port). Php_eol;
Echo Parse_url ($url, Php_url_user). Php_eol;
Echo Parse_url ($url, Php_url_pass). Php_eol;
Echo Parse_url ($url, Php_url_path). Php_eol;
Echo Parse_url ($url, Php_url_query). Php_eol;
Echo Parse_url ($url, php_url_fragment). Php_eol;
? >

Output:

Array
(
    [scheme] => HTTP
    [host] => www.fdipzone.com
    [port] =>
    [user] => Fdipzone
    [Pass] => 123456
    [path] =>/test/index.php
    [query] => id=1
    [fragment] => tag< c21/>)
http
www.fdipzone.com
fdipzone
123456
/test/index.php
id=1
Tag


3.parse_str

Parse_str parse a string into multiple variables

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

If STR is a query string that is passed in by a URL, it resolves to a variable and sets it to the current scope.


Parameters:
Str
The string entered

arr
If you set the second variable arr, the variable will be stored as an array element in the array as an alternative.


Example 1: parsing to the current scope

<?php
$str = ' name=fdipzone&gender=male&profession=programer&explain=a new programmer ';
Parse_str ($STR);
Echo $name. Php_eol;
Echo $gender. Php_eol;
Echo $profession. Php_eol;
Echo $explain. Php_eol;
? >

Output:

Fdipzone
male
programer
a new programmer



Example 2: result saved to arr array

<?php
$str = ' name=fdipzone&gender=male&profession=programer&explain=a new programmer ';
Parse_str ($str, $arr);
Print_r ($arr);
? >

Output:

Array
(
    [name] => fdipzone
    [gender] => male
    [profession] => programer
    [explain] => a New programmer
)


4. Get the query parameter of the URL and parse

First use Parse_url to get query, and then use PARSE_STR to parse the parameters

<?php
$url = ' http://www.fdipzone.com/test/index.php?name=fdipzone&gender=male&profession= Programmer&explain=a new Programmer ';
$query = Parse_url ($url, php_url_query);
Parse_str ($query, $data);
Print_r ($data);
? >

Output:

array ([name] => Fdipzone [gender] => male [profession] => Programmer [ Explain] => a new programmer) 

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.