首頁 >php教程 >php手册 >PHP的学习--解析URL,php--url

PHP的学习--解析URL,php--url

WBOY
WBOY原創
2016-06-13 09:21:35982瀏覽

PHP的学习--解析URL,php--url

PHP中有两个方法可以用来解析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 - 如 http
  • host
  • port
  • user
  • pass
  • path
  • query - 在问号 ? 之后
  • fragment - 在散列符号 # 之后

如果指定了 component 参数, parse_url() 返回一个 string (或在指定为 PHP_URL_PORT 时返回一个 integer)而不是 array。如果 URL 中指定的组成部分不存在,将会返回 NULL。

实例

<?<span>php
</span><span>$url</span> = 'http://username:password@hostname/path?arg=value#anchor'<span>;
</span><span>print_r</span>(<span>parse_url</span>(<span>$url</span><span>));
</span><span>echo</span> <span>parse_url</span>(<span>$url</span>,<span> PHP_URL_PATH);
</span>?> 

以上例程会输出:

<span>Array</span><span>
(
    [scheme] </span>=><span> http
    [host] </span>=><span> hostname
    [user] </span>=><span> username
    [pass] </span>=><span> password
    [path] </span>=> /<span>path
    [query] </span>=> arg=<span>value
    [fragment] </span>=><span> anchor
)
</span>/path

parse_str

将字符串解析成多个变量

void parse_str ( string $str [, array &$arr ] )

如果 str 是 URL 传递入的查询字符串(query string),则将它解析为变量并设置到当前作用域。

获取当前的 QUERY_STRING,你可以使用 $_SERVER['QUERY_STRING'] 变量。

参数

str  输入的字符串。

arr  如果设置了第二个变量 arr,变量将会以数组元素的形式存入到这个数组,作为替代。、

实例

<?<span>php
</span><span>$str</span> = "first=value&arr[]=foo+bar&arr[]=baz"<span>;
</span><span>parse_str</span>(<span>$str</span><span>);
</span><span>echo</span> <span>$first</span>;  <span>//</span><span> value</span>
<span>echo</span> <span>$arr</span>[0]; <span>//</span><span> foo bar</span>
<span>echo</span> <span>$arr</span>[1]; <span>//</span><span> baz</span>
<span>parse_str</span>(<span>$str</span>, <span>$output</span><span>);
</span><span>echo</span> <span>$output</span>['first'];  <span>//</span><span> value</span>
<span>echo</span> <span>$output</span>['arr'][0]; <span>//</span><span> foo bar</span>
<span>echo</span> <span>$output</span>['arr'][1]; <span>//</span><span> baz</span>
?> 

 

前一段时间在读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

代码如下:

    <span>/*</span><span>*
     * 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]
     </span><span>*/</span>
    <span>public</span> <span>static</span> <span>function</span> parseDsn(<span>$dsn</span><span>)
    {
        </span><span>if</span> (<span>$dsn</span> == ''<span>) {
            </span><span>//</span><span> Use a sensible default for an empty DNS string</span>
            <span>$dsn</span> = 'redis://' . self::<span>DEFAULT_HOST;
        }
        </span><span>$parts</span> = <span>parse_url</span>(<span>$dsn</span><span>);
        </span><span>//</span><span> Check the URI scheme</span>
        <span>$validSchemes</span> = <span>array</span>('redis', 'tcp'<span>);
        </span><span>if</span> (<span>isset</span>(<span>$parts</span>['scheme']) && ! <span>in_array</span>(<span>$parts</span>['scheme'], <span>$validSchemes</span><span>)) {
            </span><span>throw</span> <span>new</span> \InvalidArgumentException("Invalid DSN. Supported schemes are " . <span>implode</span>(', ', <span>$validSchemes</span><span>));
        }
        </span><span>//</span><span> Allow simple 'hostname' format, which `parse_url` treats as a path, not host.</span>
        <span>if</span> ( ! <span>isset</span>(<span>$parts</span>['host']) && <span>isset</span>(<span>$parts</span>['path'<span>])) {
            </span><span>$parts</span>['host'] = <span>$parts</span>['path'<span>];
            </span><span>unset</span>(<span>$parts</span>['path'<span>]);
        }
        </span><span>//</span><span> Extract the port number as an integer</span>
        <span>$port</span> = <span>isset</span>(<span>$parts</span>['port']) ? <span>intval</span>(<span>$parts</span>['port']) : self::<span>DEFAULT_PORT;
        </span><span>//</span><span> Get the database from the 'path' part of the URI</span>
        <span>$database</span> = <span>false</span><span>;
        </span><span>if</span> (<span>isset</span>(<span>$parts</span>['path'<span>])) {
            </span><span>//</span><span> Strip non-digit chars from path</span>
            <span>$database</span> = <span>intval</span>(<span>preg_replace</span>('/[^0-9]/', '', <span>$parts</span>['path'<span>]));
        }
        </span><span>//</span><span> Extract any 'user' and 'pass' values</span>
        <span>$user</span> = <span>isset</span>(<span>$parts</span>['user']) ? <span>$parts</span>['user'] : <span>false</span><span>;
        </span><span>$pass</span> = <span>isset</span>(<span>$parts</span>['pass']) ? <span>$parts</span>['pass'] : <span>false</span><span>;
        </span><span>//</span><span> Convert the query string into an associative array</span>
        <span>$options</span> = <span>array</span><span>();
        </span><span>if</span> (<span>isset</span>(<span>$parts</span>['query'<span>])) {
            </span><span>//</span><span> Parse the query string into an array</span>
            <span>parse_str</span>(<span>$parts</span>['query'], <span>$options</span><span>);
        }
        </span><span>return</span> <span>array</span><span>(
            </span><span>$parts</span>['host'],
            <span>$port</span>,
            <span>$database</span>,
            <span>$user</span>,
            <span>$pass</span>,
            <span>$options</span>,<span>
        );
    }</span>

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn