轉(zhuǎn)換過濾器

如同 string.* 過濾器,convert.* 過濾器的作用就和其名字一樣。對于指定過濾器的更多信息,請參考該函數(shù)的手冊頁。

convert.base64-encode 和 convert.base64-decode

使用這兩個過濾器等同于分別用 base64_encode()base64_decode() 函數(shù)處理所有的流數(shù)據(jù)。convert.base64-encode 支持以一個關(guān)聯(lián)數(shù)組給出的參數(shù)。如果給出了 line-length,base64 輸出將被用 line-length 個字符為 長度而截成塊。如果給出了 line-break-chars,每塊將被用給出的字符隔開。這些參數(shù)的效果和用 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() 函數(shù)處理所有的流數(shù)據(jù)。沒有和 convert.quoted-printable-encode 相對應(yīng)的函數(shù)。convert.quoted-printable-encode 支持以一個關(guān)聯(lián)數(shù)組給出的參數(shù)。除了支持和 convert.base64-encode 一樣的附加參數(shù)外, convert.quoted-printable-encode 還支持布爾參數(shù) binaryforce-encode-first。 convert.base64-decode 只支持 line-break-chars 參數(shù)作為從編碼載荷中剝離的類型提示。

示例 #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() 處理所有的流數(shù)據(jù)。 該過濾器不支持參數(shù),但可使用輸入/輸出的編碼名稱,組成過濾器名稱,比如 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. */
?>