轉換過濾器
如同 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
還支援布爾參數
binary
和
force-encode-first
。
convert.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. */
?>