国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

php中2個(gè)解析URL的方法

墨辰丷
發(fā)布: 2018-06-13 10:57:09
原創(chuàng)
2268人瀏覽過

本文主要向大家介紹了php中2個(gè)解析url的方法(parse_url和parse_str),以及這2種方法的簡(jiǎn)介和用法,十分全面,推薦給有需要的小伙伴們。

PHP中有兩個(gè)方法可以用來解析URL,分別是parse_url和parse_str。

parse_url解析 URL,返回其組成部分

mixed parse_url ( string $url [, int $component = -1 ] )

本函數(shù)解析一個(gè) URL 并返回一個(gè)關(guān)聯(lián)數(shù)組,包含在 URL 中出現(xiàn)的各種組成部分。

立即學(xué)習(xí)PHP免費(fèi)學(xué)習(xí)筆記(深入)”;

本函數(shù)不是用來驗(yàn)證給定 URL 的合法性的,只是將其分解為下面列出的部分。不完整的 URL 也被接受,parse_url() 會(huì)嘗試盡量正確地將其解析。

參數(shù)

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 的其中一個(gè)來獲取 URL 中指定的部分的 string。 (除了指定為 PHP_URL_PORT 后,將返回一個(gè) integer 的值)。

返回值

對(duì)嚴(yán)重不合格的 URL,parse_url() 可能會(huì)返回 FALSE。

如果省略了 component 參數(shù),將返回一個(gè)關(guān)聯(lián)數(shù)組 array,在目前至少會(huì)有一個(gè)元素在該數(shù)組中。數(shù)組中可能的鍵有以下幾種:

scheme - 如 http
host
port
user
pass
path
query - 在問號(hào) ? 之后
fragment - 在散列符號(hào) # 之后
如果指定了 component 參數(shù), parse_url() 返回一個(gè) string (或在指定為 PHP_URL_PORT 時(shí)返回一個(gè) integer)而不是 array。如果 URL 中指定的組成部分不存在,將會(huì)返回 NULL。

實(shí)例

代碼如下:

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
登錄后復(fù)制

以上例程會(huì)輸出:

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)
/path
登錄后復(fù)制

parse_str

將字符串解析成多個(gè)變量

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

如果 str 是 URL 傳遞入的查詢字符串(query string),則將它解析為變量并設(shè)置到當(dāng)前作用域。

獲取當(dāng)前的 QUERY_STRING,你可以使用 $_SERVER['QUERY_STRING'] 變量。

參數(shù)

str? 輸入的字符串。

arr? 如果設(shè)置了第二個(gè)變量 arr,變量將會(huì)以數(shù)組元素的形式存入到這個(gè)數(shù)組,作為替代。、

實(shí)例

代碼如下:

<?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
?>
登錄后復(fù)制

前一段時(shí)間在讀php-resque的源碼,看到了在其中對(duì)這兩個(gè)的方法的應(yīng)用,感覺用的很好,用來解析redis鏈接的設(shè)置。

redis鏈接的格式是:redis://user:pass@host:port/db?option1=val1&option2=val2,是不是和URL一樣,所以用以上兩個(gè)方法很容易解析。

地址: 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,
        );
    }
登錄后復(fù)制

總結(jié):以上就是本篇文的全部?jī)?nèi)容,希望能對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)推薦:

php如何利用array_merge()函數(shù)合并兩個(gè)數(shù)組

PHP實(shí)現(xiàn)批量生成各種尺寸Logo的方法

PHP使用pcntl函數(shù)操作多進(jìn)程的方法

以上就是php中2個(gè)解析URL的方法的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!

PHP速學(xué)教程(入門到精通)
PHP速學(xué)教程(入門到精通)

PHP怎么學(xué)習(xí)?PHP怎么入門?PHP在哪學(xué)?PHP怎么學(xué)才快?不用擔(dān)心,這里為大家提供了PHP速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!

下載
來源:php中文網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn
最新問題
開源免費(fèi)商場(chǎng)系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)