Talking about PHP parsing URL function parse_url and parse_str,parse_urlparse_str
There are two methods in PHP that can be used to parse URLs, namely Parse_url and PARSE_STR.
Parse_url
Parse the URL and return its components
Mixed Parse_url (string $url [, int $component =-1])
This function parses a URL and returns an associative array containing the various components that appear in the URL.
This function is not used to validate the legitimacy of a given URL, but to decompose it into the parts listed below. An incomplete URL is also accepted, and Parse_url () tries to parse it as correctly as possible.
Parameters
The URL to parse for the URL. Invalid characters will be replaced with _.
Component Specify 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 one of the strings that gets the specified part of the URL. (An integer value is returned, except when specified as Php_url_port).
return value
A url,parse_url () that is severely unqualified may return FALSE.
If the component parameter is omitted, an associative array array is returned, at least one element is currently in the array. There are several possible keys in the array:
Scheme-such as HTTP
Host
Port
User
Pass
Path
Query-in question marks? After
Fragment-After hash symbol #
If the component parameter is specified, Parse_url () returns a string (or returns an integer when specified as Php_url_port) instead of an array. If the component specified in the URL does not exist, NULL will be returned.
Instance
Copy the Code code as follows:
<?php
$url = ' Http://username:password@hostname/path?arg=value#anchor ';
Print_r (Parse_url ($url));
Echo Parse_url ($url, Php_url_path);
?>
The above routines will output:
Copy the Code code as follows:
Array
(
[Scheme] = http
[Host] = hostname
[User] = Username
[Pass] = password
[Path] =/path
[Query] = Arg=value
[Fragment] = Anchor
)
/path
Parse_str
Parsing a string into multiple variables
void Parse_str (String $str [, Array & $arr])
If STR is a query string that the URL passes in, it resolves to a variable and sets it to the current scope.
To get the current query_string, you can use the $_server[' query_string '] variable.
Parameters
The string entered by Str.
Arr If the second variable is set to ARR, the variable will be stored as an array element in the array as an alternative. 、
Instance
Copy the Code code as follows:
<?php
$str = "First=value&arr[]=foo+bar&arr[]=baz";
Parse_str ($STR);
Echo $first; Value
echo $arr [0]; Foo Bar
echo $arr [1]; Baz
Parse_str ($str, $output);
echo $output [' first ']; Value
echo $output [' arr '][0]; Foo Bar
echo $output [' arr '][1]; Baz
?>
A period of time before reading Php-resque source code, saw in which the two methods of application, feel good, used to parse Redis link settings.
The format of the Redis link is: Redis://user:pass@host:port/db?option1=val1&option2=val2, is not the same as the URL, so with the above two methods are easy to parse.
Address: https://github.com/chrisboulton/php-resque/blob/master/lib/Resque/Redis.php
The code is as follows:
Copy CodeThe code is as follows:
/**
* Parse a DSN string, which can have one of the following formats:
*
*-Host:port
*-Redis://user:pass@host:port/db?option1=val1&option2=val2
*-Tcp://user:pass@host:port/db?option1=val1&option2=val2
*
* note:the ' user ' part of the DSN was not used.
*
* @param string $dsn A DSN string
* @return Array An array of DSN compotnents, with ' false ' values for any unknown components. e.g.
* [Host, port, DB, User, pass, options]
*/
public static function Parsedsn ($DSN)
{
if ($dsn = = ") {
Use a sensible the default for an empty DNS string
$dsn = ' redis://'. Self::D efault_host;
}
$parts = Parse_url ($DSN);
Check the URI scheme
$validSchemes = Array (' Redis ', ' TCP ');
if (Isset ($parts [' scheme ')] && in_array ($parts [' scheme '], $validSchemes)) {
throw new \invalidargumentexception ("Invalid DSN. Supported schemes is ". Implode (', ', $validSchemes));
}
Allow simple ' hostname ' format, which ' Parse_url ' treats as a path, not host.
if (! isset ($parts [' Host ']) && isset ($parts [' path ']) {
$parts [' host '] = $parts [' path '];
unset ($parts [' path ']);
}
Extract the port number as an integer
$port = Isset ($parts [' Port '])? Intval ($parts [' Port ']): self::D efault_port;
Get the database from the ' path ' part of the URI
$database = false;
if (Isset ($parts [' path ')]) {
Strip non-digit chars from Path
$database = Intval (preg_replace ('/[^0-9]/', ' ', $parts [' path ')];
}
Extract any ' user ' and ' pass ' values
$user = Isset ($parts [' user '])? $parts [' User ']: false;
$pass = Isset ($parts [' Pass '])? $parts [' Pass ']: false;
Convert the query string into an associative array
$options = Array ();
if (Isset ($parts [' query '])) {
Parse the query string into an array
Parse_str ($parts [' query '], $options);
}
Return Array (
$parts [' Host '],
$port,
$database,
$user,
$pass,
$options,
);
}
The above is a personal understanding of PHP parsing URL function parse_url and parse_str, here to record, share to everyone, hope to help small partners
http://www.bkjia.com/PHPjc/909340.html www.bkjia.com true http://www.bkjia.com/PHPjc/909340.html techarticle on PHP parsing URL functions parse_url and parse_str,parse_urlparse_str PHP have two methods that can be used to parse URLs, respectively, Parse_url and Parse_str. Parse_url parse URL, return its composition ...