自訂的parse_url逆向函數http_build_url,將數組轉為url字串

來源:互聯網
上載者:User

標籤:

parse_url函數可以講一個url字串解析成一個數組,但是php中似乎沒有parse_url的逆向函數來講一個解析出來的數組組合成url字串,只有一個http_build_query用來將數組組合成query string。於是我自己寫了一個http_build_url函數將parse_url解析出來的數組組合成完整的url。

/** * 根據parse_url格式的數組產生完整的url * @param array $arr 接受parse_url解析出來的所有參數,完整參數執行個體如下: *        Array *        ( *            [scheme] => http            // 協議 *            [host] => www.baidu.com     // 主機 *            [port] => 80                // 連接埠,可選 *            [path] => /path/file.php    // 路徑(檔案名稱),可選 *            [query] => a=aaa&b=aaabbb    // 參數(query string),可選 *            [fragment] => 123            // 附加部分或者叫做錨點(#後面的),可選 *        )*/function http_build_url($url_arr){    $new_url = $url_arr[‘scheme‘] . "://".$url_arr[‘host‘];    if(!empty($url_arr[‘port‘]))        $new_url = $new_url.":".$url_arr[‘port‘];    $new_url = $new_url . $url_arr[‘path‘];    if(!empty($url_arr[‘query‘]))        $new_url = $new_url . "?" . $url_arr[‘query‘];    if(!empty($url_arr[‘fragment‘]))        $new_url = $new_url . "#" . $url_arr[‘fragment‘];    return $new_url;}

使用測試:

$url = "http://www.dragondean.cn:80/path/file.php?a=aaa&b=bcdefg#123";$url_arr = parse_url($url);print_r($url_arr);        $url_new = http_build_url($url_arr);echo $url_new;

運行結果:

Array(    [scheme] => http    [host] => www.dragondean.cn    [port] => 80    [path] => /path/file.php    [query] => a=aaa&b=bcdefg    [fragment] => 123)http://www.dragondean.cn:80/path/file.php?a=aaa&b=bcdefg#123

 

自訂的parse_url逆向函數http_build_url,將數組轉為url字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.