php中url函數介紹及使用

來源:互聯網
上載者:User

 本文主要介紹了PHP處理url字串編碼、解碼與解析。下面進行詳細的舉例說明

base64_encode — 使用 MIME base64 對資料進行編碼base64_encode() returns 使用 base64 對 data 進行編碼。設計此種編碼是為了使位元據可以通過非純 8-bit 的傳輸層傳輸,例如電子郵件的主體。Base64-encoded 資料要比未經處理資料多佔用 33% 左右的空間。 代碼如下:<?php$str = 'This is an encoded string';// VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==echo base64_encode($str);?>   base64_decode — 對使用 MIME base64 編碼的資料進行解碼base64_decode() 對 encoded_data 進行解碼,返回未經處理資料,失敗則返回 FALSE。返回的資料可能是二進位的。 代碼如下:<?php$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';// This is an encoded stringecho base64_decode($str);?>   get_headers — 取得伺服器響應一個 HTTP 要求所發送的所有標題get_headers() 返回一個數組,包含有伺服器響應一個 HTTP 要求所發送的標題。如果失敗則返回 FALSE 並發出一條 E_WARNING 層級的錯誤資訊。如果將可選的 format 參數設為 1,則 get_headers() 會解析相應的資訊並設定數組的鍵名。  代碼如下:<?php$phpha1 = get_headers('http://www.jb51.net');$phpha2 = get_headers('http://www.jb51.net', 1);print_r($phpha1);print_r($phpha2);?>  輸出如下:  代碼如下:Array(    [0] => HTTP/1.1 200 OK    [1] => Server: nginx/1.2.2    [2] => Date: Tue, 06 Nov 2012 10:17:59 GMT    [3] => Content-Type: text/html; charset=UTF-8    [4] => Connection: close    [5] => X-Powered-By: PHP/5.3.8    [6] => X-Pingback: http://www.jb51.net/xmlrpc.php    [7] => Via: 10.67.15.26    [8] => Set-Cookie: saeut=124.127.138.35.1352197078737175; path=/; max-age=311040000)Array(    [0] => HTTP/1.1 200 OK    [Server] => nginx/1.2.2    [Date] => Tue, 06 Nov 2012 10:17:59 GMT    [Content-Type] => text/html; charset=UTF-8    [Connection] => close    [X-Powered-By] => PHP/5.3.8    [X-Pingback] => http://www.jb51.net/xmlrpc.php    [Via] => 10.67.15.21    [Set-Cookie] => saeut=124.127.138.35.1352197079055460; path=/; max-age=311040000)  get_meta_tags — 從一個檔案中提取所有的 meta 標籤 content 屬性,返回一個數組可以想象的到,某些網站可以方便的用此函數進行網站SEO資訊的提取。  代碼如下:<?php//天涯PHP部落格 http://www.jb51.net$phpha = get_meta_tags('http://www.jb51.net');print_r($phpha);?>  輸出如下:  代碼如下:Array(    [keywords] => 天涯部落格,PHP部落格,PHP技術部落格,PHP學習部落格,PHP開發部落格    [description] => 天涯PHP部落格是以PHP為主的學習部落格,記載PHPER的學習曆程,關注互連網最新發展動態。    [generator] => WordPress 3.2.1)  http_build_query — 產生 URL-encode 之後的請求字串 <?php$url = array('c'=>'blog', 'a'=>'show', 'id'=>10, 'hello', 'world');// c=blog&a=show&id=10&0=hello&1=worldecho http_build_query($url);// c=blog&a=show&id=10&phpha_0=hello&phpha_1=worldecho http_build_query($url, 'jb51_');?>[/code] 這個函數目前我用的最多的地方就是做各種API時,組合請求的url,非常的方便。另外可以看到,對於數組內數字索引的成員,還可以指定首碼。 parse_url — 解析 URL,返回其組成部分本函數解析一個 URL 並返回一個關聯陣列,包含在 URL 中出現的各種組成部分。本函數不是用來驗證給定 URL 的合法性的,只是將其分解為下面列出的部分。不完整的 URL 也被接受,parse_url() 會嘗試盡量正確地將其解析。  代碼如下:<?php$url = 'http://tianya:jb51.net@jb51.com/hello.php?id=10#nav';print_r(parse_url($url));?>Array(    [scheme] => http    [host] => phpha.com    [user] => tianya    [pass] => phphadotcom    [path] => /hello.php    [query] => id=10    [fragment] => nav)  rawurlencode — 按照 RFC 1738 對 URL 進行編碼rawurldecode — 對已編碼的 URL 字串進行解碼urlencode — 編碼 URL 字串urldecode — 解碼已編碼的 URL 字串  代碼如下:<?php$url = 'http://www.jb51.net tianya';echo urlencode($url);echo '<br />';echo rawurlencode($url);echo '<br />';echo urldecode($url);echo '<br />';echo rawurldecode($url);?>  輸出如下:  代碼如下:http%3A%2F%2Fwww.jb51.net+tianyahttp%3A%2F%2Fwww.jb51.net%20tianya  可以看到,urlencode與rawurlencode的區別在於:urlencode() 會把空格編碼為加號(+),rawurlencode() 則把空格編碼為 %20urldecode()和rawurldecode() 則為對應的解碼函數。 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.