Php uses http_build_query, parse_url, and parse_str to create and parse URLs. if you need them, refer. 1. http_build_queryHttp_build_query can be used to create a request string after urlencode.
- 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
- $ Data = array (
- 'Name' => 'fdipzone ',
- 'Gender' => 'male ',
- 'Comprehension' => 'grammer ',
- 'Explain '=> 'A new programmer'
- );
- Echo http_build_query ($ data );
- ?>
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
- $ Data = array ('fdipzone ', 'male', 'grammer', '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 % 20 programmer
2. parse_urlParse_url parses the url and returns its components
- 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:
- $ 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] => 80
- [User] => fdipzone
- [Pass] = & gt; 123456
- [Path] =>/test/index. php
- [Query] => id = 1
- [Fragment] => tag
- )
- Http
- Www.fdipzone.com
- 80
- Fdipzone
- 123456
- /Test/index. php
- Id = 1
- Tag
3. parse_strParse_str parses the string into multiple variables
- 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
- $ Str = 'name = fdipzone & gender = male & compression Sion = programer & explain = a new programmer ';
- Parse_str ($ str );
- Echo $ name. PHP_EOL;
- Echo $ gender. PHP_EOL;
- Echo $ regression Sion. PHP_EOL;
- Echo $ explain. PHP_EOL;
- ?>
Output:
- Fdipzone
- Male
- Programer
- A new programmer
Example 2: save the result to the arr array
- $ Str = 'name = fdipzone & gender = male & compression Sion = programer & explain = a new programmer ';
- Parse_str ($ str, $ arr );
- Print_r ($ arr );
- ?>
Output:
- Array
- (
- [Name] => fdipzone
- [Gender] => male
- [Syntax Sion] => programer
- [Explain] => a new programmer
- )
4. obtain and parse the url query parametersFirst, use parse_url to obtain the query, and then parse the parameters using parse_str.
- $ Url = 'http: // www.fdipzone.com/test/index.php? Name = fdipzone & gender = male & compression Sion = 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
- [Projection] => programmer
- [Explain] => a new programmer
- )
|