Home | Mirror | SearchITEYE 博客 | OSChina 博客 | 51CTO 博客

4.2. PHP Base64

4.2.1. base64_encode

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==  */
?>
		
			

4.2.2. base64_decode

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

comments powered by Disqus