Heim  >  Artikel  >  Backend-Entwicklung  >  PHP内置封装协议之php://filter

PHP内置封装协议之php://filter

WBOY
WBOYOriginal
2016-06-23 14:29:532150Durchsuche

php://filter 是一种设计用来允许过滤器程序在打开时成为流的封装协议。这对于单独具有完整功能的文件函数例如 readfile(),file() 和 file_get_contents() 很有用,否则就没有机会在读取内容之前将过滤器应用于流之上。

 

该协议语法为 

php://filter:/=

比如   php://filter:/resource=http://www.baidu.com

 

使用 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 该怎么做去官网看看吧.

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn