转换过滤器

如同 string.* 过滤器,convert.* 过滤器的作用就和其名字一样。对于指定过滤器的更多信息,请参考该函数的手册页。

convert.base64-encode 和 convert.base64-decode

使用这两个过滤器等同于分别用 base64_encode()base64_decode() 函数处理所有的流数据。convert.base64-encode 支持以一个关联数组给出的参数。如果给出了 line-length,base64 输出将被用 line-length 个字符为长度而截成块。如果给出了 line-break-chars,每块将被用给出的字符隔开。这些参数的效果和用 base64_encode() 再加上 chunk_split() 相同。

示例 #1 convert.base64-encode & convert.base64-decode

<?php
$fp
= fopen('php://output', 'w');
stream_filter_append($fp, 'convert.base64-encode');
fwrite($fp, "This is a test.\n");
fclose($fp);
/* 输出: VGhpcyBpcyBhIHRlc3QuCg== */

$param = array('line-length' => 8, 'line-break-chars' => "\r\n");
$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'convert.base64-encode', STREAM_FILTER_WRITE, $param);
fwrite($fp, "This is a test.\n");
fclose($fp);
/* 输出: VGhpcyBp
: cyBhIHRl
: c3QuCg== */

$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'convert.base64-decode');
fwrite($fp, "VGhpcyBpcyBhIHRlc3QuCg==");
fclose($fp);
/* 输出: This is a test. */
?>

convert.quoted-printable-encode 和 convert.quoted-printable-decode

使用此过滤器的 decode 版本等同于用 quoted_printable_decode() 函数处理所有的流数据。没有和 convert.quoted-printable-encode 相对应的函数。convert.quoted-printable-encode 支持以一个关联数组给出的参数。除了支持和 convert.base64-encode 一样的附加参数外,convert.quoted-printable-encode 还支持布尔参数 binaryforce-encode-firstconvert.base64-decode 只支持 line-break-chars 参数作为从编码载荷中剥离的类型提示。

示例 #2 convert.quoted-printable-encode & convert.quoted-printable-decode

<?php
$fp
= fopen('php://output', 'w');
stream_filter_append($fp, 'convert.quoted-printable-encode');
fwrite($fp, "This is a test.\n");
/* 输出: =This is a test.=0A */
?>

convert.iconv.*

在激活 iconv 的前提下可以使用 convert.iconv.* 压缩过滤器, 等同于用 iconv() 处理所有的流数据。 该过滤器不支持参数,但可使用输入/输出的编码名称,组成过滤器名称,比如 convert.iconv.<input-encoding>.<output-encoding>convert.iconv.<input-encoding>/<output-encoding> (两种写法的语义都相同)。

示例 #3 convert.iconv.*

<?php
$fp
= fopen('php://output', 'w');
stream_filter_append($fp, 'convert.iconv.utf-16le.utf-8');
fwrite($fp, "T\0h\0i\0s\0 \0i\0s\0 \0a\0 \0t\0e\0s\0t\0.\0\n\0");
fclose($fp);
/* 输出:This is a test. */
?>
add a note

User Contributed Notes 1 note

up
-2
marcus at synchromedia dot co dot uk
2 years ago
It's not quite obvious what all the available parameters are for convert.quoted-printable-encode. If you want the stream filter to act the same way as the quoted_printable_encode function, you need these extra params, for example:

stream_filter_append(
STDOUT,
'convert.quoted-printable-encode',
STREAM_FILTER_WRITE,
[
'line-break-chars' => PHP_EOL,
'line-length' => 75,
]
);
echo stream_copy_to_stream(STDIN, STDOUT);

Without these extra params set, you may get no wrapping at all, or wrapping using the wrong line break sequence.
To Top