base64_encode

(PHP 4, PHP 5, PHP 7)
base64_encode — 使用 MIME base64 对数据进行编码

说明

base64_encode ( string $data ) : string
使用 base64 对 data 进行编码。
设计此种编码是为了使二进制数据可以通过非纯 8-bit 的传输层传输,例如电子邮件的主体。
Base64-encoded 数据要比原始数据多占用 33% 左右的空间。

参数

data
要编码的数据。

返回值

编码后的字符串数据, 或者在失败时返回 false

范例

示例 #1 base64_encode() 示例
<?php<br />$str = 'This is an encoded string';<br />echo base64_encode($str);<br />?>
以上例程会输出:
VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==

参见

A function I’m using to return local images as base64 encrypted code, i.e. embedding the image source into the html request.

This will greatly reduce your page load time as the browser will only need to send one server request for the entire page, rather than multiple requests for the HTML and the images. Requests need to be uploaded and 99% of the world are limited on their upload speed to the server.

<?php
function base64_encode_image ($filename=string,$filetype=string) {
if ($filename) {
$imgbinary = fread(fopen($filename, “r”), filesize($filename));
return ‘data:image/‘ . $filetype . ‘;base64,’ . base64_encode($imgbinary);
}
}
?>

used as so

or

base64 - 图1

base64_decode

(PHP 4, PHP 5, PHP 7)
base64_decode — 对使用 MIME base64 编码的数据进行解码

说明

base64_decode ( string $data [, bool $strict = false ] ) : string
对 base64 编码的 data 进行解码。

参数

data
编码过的数据。
strict
当设置 stricttrue 时,一旦输入的数据超出了 base64 字母表,将返回 false。 否则会静默丢弃无效的字符。

返回值

返回原始数据, 或者在失败时返回 false。返回的数据可能是二进制的。

更新日志

版本 说明
5.2.0 增加了 strict

范例

示例 #1 base64_decode() 示例
<?php<br />$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';<br />echo base64_decode($str);<br />?>
以上例程会输出:
This is an encoded string