首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

stream_filter_append

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

stream_filter_append - 将过滤器附加到流中

描述

代码语言:javascript
复制
resource stream_filter_append ( resource $stream , string $filtername [, int $read_write [, mixed $params ]] )

添加filtername到附加的过滤器列表中stream

参数

stream

目标流。

filtername

过滤器名称。

read_write

默认情况下,如果打开文件(即文件模式:r和/或+),stream_filter_append()会将过滤器附加到读取过滤器链。如果文件被打开进行写入(即文件模式:wa和/或+),则过滤器也将附加到写入过滤器链中。,和/或也可以传递给参数以覆盖此行为。STREAM_FILTER_READSTREAM_FILTER_WRITESTREAM_FILTER_ALLread_write

params

该过滤器将与指定的列表一起添加params到列表的末尾,因此将在流操作期间最后被调用。要将过滤器添加到列表的开头,请使用stream_filter_prepend()。

返回值

返回成功或FALSE失败时的资源。在调用stream_filter_remove()期间,资源可用于引用此过滤器实例。

如果stream不是资源或者filtername不能定位,则返回FALSE

Changelog

版本

描述

5.1.0

在PHP 5.1.0之前,此函数在成功返回TRUE或失败时返回FALSE。

例子

示例#1控制应用过滤器的位置

代码语言:javascript
复制
<?php
/* Open a test file for reading and writing */
$fp = fopen('test.txt', 'w+');

/* Apply the ROT13 filter to the
 * write filter chain, but not the
 * read filter chain */
stream_filter_append($fp, "string.rot13", STREAM_FILTER_WRITE);

/* Write a simple string to the file
 * it will be ROT13 transformed on the
 * way out */
fwrite($fp, "This is a test\n");

/* Back up to the beginning of the file */
rewind($fp);

/* Read the contents of the file back out.
 * Had the filter been applied to the
 * read filter chain as well, we would see
 * the text ROT13ed back to its original state */
fpassthru($fp);

fclose($fp);

/* Expected Output
   ---------------

Guvf vf n grfg

 */
?>

注意

注意使用自定义(用户)过滤器时, 必须首先调用stream_filter_register(),以便将所需的用户过滤器注册为filtername

注意:流数据是从资源(包括本地和远程)中以块读取的,任何未使用的数据保存在内部缓冲区中。当一个新的过滤器被附加到流中时,内部缓冲区中的数据将在那个时候通过新的过滤器进行处理。这与stream_filter_prepend()的行为不同。

注意:添加过滤器进行读取和写入时,会创建两个过滤器实例。stream_filter_append()必须被调用两次,STREAM_FILTER_READSTREAM_FILTER_WRITE获取两个过滤器资源。

另请参阅

  • stream_filter_register() - 注册一个用户定义的流过滤器
  • stream_filter_prepend() - 将过滤器附加到流中
  • stream_get_filters() - 检索已注册过滤器的列表

← stream_encoding

stream_filter_prepend →

扫码关注腾讯云开发者

领取腾讯云代金券