base64_encode
(PHP 3, PHP 4, PHP 5)
base64_encode -- 使用 MIME base64 对数据进行编码
说明
string base64_encode ( string data )
base64_encode() returns 使用 base64 对 data 进行编码。设计此种编码是为了使二进制数据可以通过非纯 8-bit 的传输层传输,例如电子邮件的主体。
Base64-encoded 数据要比原始数据多占用 33% 左右的空间。
例子 1. base64_encode() 示例
<?php $str = 'This is an encoded string'; echo base64_encode($str); ?>
此示例将显示:
VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
例子 2. stream_filter_append() 示例
<?php
$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'convert.base64-encode');
fwrite($fp, "This is a test.\n");
fclose($fp);
/* Outputs: VGhpcyBpcyBhIHRlc3QuCg== */
echo "\n============================================\n";
$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'convert.base64-decode');
fwrite($fp, "VGhpcyBpcyBhIHRlc3QuCg==");
fclose($fp);
/* Outputs: This is a test. */
echo "============================================\n";
$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);
/* Outputs: VGhpcyBp
: cyBhIHRl
: c3QuCg== */
?>
base64_decode
(PHP 3, PHP 4, PHP 5)
base64_decode -- 对使用 MIME base64 编码的数据进行解码
说明
string base64_decode ( string encoded_data )
base64_decode() 对 encoded_data 进行解码,返回原始数据,失败则返回 FALSE。返回的数据可能是二进制的。
例子 1. base64_decode() 示例
<?php $str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=='; echo base64_decode($str); ?>
此示例将显示:
This is an encoded string