php的学习--解析url,php--urlphp中有两个方法可以用来解析url,分别是parse_url和parse_str。
parse_url解析 url,返回其组成部分
mixed parse_url ( string $url [, int $component = -1 ] )
本函数解析一个 url 并返回一个关联数组,包含在 url 中出现的各种组成部分。
本函数不是用来验证给定 url 的合法性的,只是将其分解为下面列出的部分。不完整的 url 也被接受,parse_url() 会尝试尽量正确地将其解析。
参数
url 要解析的 url。无效字符将使用 _ 来替换。
component 指定 php_url_scheme、 php_url_host、 php_url_port、 php_url_user、 php_url_pass、 php_url_path、 php_url_query 或 php_url_fragment 的其中一个来获取 url 中指定的部分的 string。 (除了指定为 php_url_port 后,将返回一个 integer 的值)。
返回值
对严重不合格的 url,parse_url() 可能会返回 false。
如果省略了 component 参数,将返回一个关联数组 array,在目前至少会有一个元素在该数组中。数组中可能的键有以下几种:
scheme - 如 httphostportuserpasspathquery - 在问号 ? 之后fragment - 在散列符号 # 之后如果指定了 component 参数, parse_url() 返回一个 string (或在指定为 php_url_port 时返回一个 integer)而不是 array。如果 url 中指定的组成部分不存在,将会返回 null。
实例
以上例程会输出:
array( [scheme] => http [host] => hostname [user] => username [pass] => password [path] => /path [query] => arg=value [fragment] => anchor)/path
parse_str将字符串解析成多个变量
void parse_str ( string $str [, array &$arr ] )
如果 str 是 url 传递入的查询字符串(query string),则将它解析为变量并设置到当前作用域。
获取当前的 query_string,你可以使用 $_server['query_string'] 变量。
参数
str 输入的字符串。
arr 如果设置了第二个变量 arr,变量将会以数组元素的形式存入到这个数组,作为替代。、
实例
前一段时间在读php-resque的源码,看到了在其中对这两个的方法的应用,感觉用的很好,用来解析redis链接的设置。
redis链接的格式是:redis://user:pass@host:port/db?option1=val1&option2=val2,是不是和url一样,所以用以上两个方法很容易解析。
地址: https://github.com/chrisboulton/php-resque/blob/master/lib/resque/redis.php
代码如下:
/** * 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 is 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 default for an empty dns string $dsn = 'redis://' . self::default_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 are . 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::default_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, ); }
http://www.bkjia.com/phpjc/909086.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/909086.htmltecharticlephp的学习--解析url,php--url php中有两个方法可以用来解析url,分别是parse_url和parse_str。 parse_url 解析 url,返回其组成部分 mixed parse_url ( str...