PHP URL函数

PHP base64_encode() 函数使用 MIME(多用于 Internet 邮件扩展)对给定的字符串进行base64 编码。此编码旨在使二进制数据能够通过非 8 位干净的传输层(例如邮件正文)进行传输。 Base64 编码的数据比原始数据多占用约 33% 的空间。

语法

base64_encode(string) 

参数

string必填。 指定要编码的字符串。

返回值

以字符串形式返回编码数据。

示例:

下面的示例显示了base64_encode()函数的用法。

<?php
$str = "Yxjc123";
$encoded_str = base64_encode($str);

//显示编码后的字符串
echo $encoded_str;
?> 

上述代码的输出将是:

WXhqYzEyMw==

示例:

再考虑一个示例,它显示了字符串的编码方式以及使用 MIME base64 进行解码。

<?php
$str1 = "Programming is fun";
$encoded_str1 = base64_encode($str1);

echo "The string is: $str1 \n";
echo "Encoded string is: $encoded_str1 \n";

$str2 = "UHJvZ3JhbW1pbmcgaXMgZnVu";
$decoded_str2 = base64_decode($str2);

echo "\nThe string is: $str2 \n";
echo "Decoded string is: $decoded_str2 \n";
?> 

上述代码的输出将是:

The string is: Programming is fun 
Encoded string is: UHJvZ3JhbW1pbmcgaXMgZnVu 

The string is: UHJvZ3JhbW1pbmcgaXMgZnVu 
Decoded string is: Programming is fun