CakeFest 2024: The Official CakePHP Conference

変換フィルタ

string.* フィルタと同様、convert.* フィルタもその名前と同じような 動作をします。 これらのフィルタについてのより詳しい情報は、対応する関数のマニュアルを 参照ください。

convert.base64-encode と convert.base64-decode による変換

これらのフィルタは、すべてのストリームデータに対してそれぞれ base64_encode() または base64_decode() 関数を適用するのと同じ動作をします。 convert.base64-encode は、パラメータを連想配列形式で 受け取ることができます。line-length が与えられた 場合、base64 出力は line-length 文字単位に 分割されます。 line-break-chars が与えられた場合、 分割されたデータは、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

これらのフィルタのうち、デコードフィルタはすべてのストリームデータに 対して quoted_printable_decode() 関数を適用するのと 同じ動作をします。 convert.quoted-printable-encode に対応する関数は存在しません。 convert.quoted-printable-encode は、パラメータを 連想配列形式で受け取ることができます。convert.base64-encode でサポートされているパラメータに加え、convert.quoted-printable-encodebinaryforce-encode-first という2つの論理型パラメータをサポートしています。 convert.base64-decodeline-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.*

convert.iconv.* フィルタは、 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
0
marcus at synchromedia dot co dot uk
1 year 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