php://filter 是一种设计用来允许过滤器程序在打开时成为流的封装协议。这对于单独具有完整功能的文件函数例如 readfile(),file() 和 file_get_contents() 很有用,否则就没有机会在读取内容之前将过滤器应用于流之上。
该协议语法为
php://filter:/
比如 php://filter:/resource=http://www.baidu.com
立即学习“PHP免费学习笔记(深入)”;
使用 php://filter 获取网页内容
<p class="sycode"> <? php $url = ' http://www.phpfamily.cn ' ; $data = file_get_contents ( ' php://filter/resource= ' . $url ); echo $data ; //输出结果我http://www.phpfamily.cn页面的内容 </p>
php://filter 的 参数列表
read 读取
write 写入
resource 数据来源(必须的)
read参数值可为
string.strip_tags 将数据流中的所有html标签清除
string.toupper 将数据流中的内容转换为大写
string.tolower 将数据流中的内容转换为小写
convert.base64-encode 将数据流中的内容转换为base64编码
convert.base64-decode 与上面对应解码
例子:
<p class="sycode"> <? php $url = ' http://www.phpfamily.cn ' ; $data = file_get_contents ( ' php://filter/read=string.strip_tags/resource= ' . $url ); echo $data ; //输出结果为过滤html标签后的存文本内容 </p>
使用string.strip_tags处理数据可以媲美strip_tags函数, 因为在获取数据时已经同步操作了..
其他参数使用方法是一样的.
还有很多这样的过滤器封装协议, 详细的可以到php官方网站查看
http://docs.php.net/manual/zh/wrappers.php
还自己创建这样的流封装器
使用到的函数 stream_wapper_register 该怎么做去官网看看吧.
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号