PHP內建封裝協議之php://filter
php://filter 是一種設計用來允許過濾器程式在開啟時成為流的封裝協議。這對於單獨具有完整功能的檔案函式例如 readfile(),file() 和 file_get_contents() 很有用,否則就沒有機會在讀取內容之前將過濾器應用於流之上。
該協議語法為
php://filter:/<action>=<name>
比如 php://filter:/resource=http://www.baidu.com
使用 php://filter 獲取網頁內容
<?php $url = 'http://www.phpfamily.cn'; $data = file_get_contents('php://filter/resource=' . $url); echo $data; //輸出結果我http://www.phpfamily.cn頁面的內容
php://filter 的 引數列表
read 讀取
write 寫入
resource 資料來源(必須的)
read引數值可為
string.strip_tags 將資料流中的所有html標籤清除 string.toupper 將資料流中的內容轉換為大寫 string.tolower 將資料流中的內容轉換為小寫
convert.base64-encode 將資料流中的內容轉換為base64編碼
convert.base64-decode 與上面對應解碼
例子:
<?php $url = 'http://www.phpfamily.cn'; $data = file_get_contents('php://filter/read=string.strip_tags/resource=' . $url); echo $data; //輸出結果為過濾html標籤後的存文字內容
使用string.strip_tags處理資料可以媲美strip_tags函式, 因為在獲取資料時已經同步操作了..
其他引數使用方法是一樣的.
還有很多這樣的過濾器封裝協議, 詳細的可以到php官方網站檢視
還自己建立這樣的流封裝器
使用到的函式 stream_wapper_register 該怎麼做去官網看看吧.