Data Encoding Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
PHP Built-In Implementation of Base64
This section provides a test program for the default PHP implementation of the Base64 encoding algorithm - base64_encode() and base64_decode() functions.
The Base64 encoding algorithm bas been implemented in PHP language as 2 built-in functions since PHP 4:
To test these built-in functions, I wrote this simple testing program:
<?php # Base64_Encode_Test.php # Copyright (c) 2010 HerongYang.com. All Rights Reserved. echo "Test 1:\n"; test("A", "QQ=="); echo "Test 2:\n"; test("AB", "QUI="); echo "Test 3:\n"; test("ABC", "QUJD"); echo "Test 4:\n"; $theInput = "The quick brown fox jumps over the lazy dog."; $theExpected = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4="; test($theInput, $theExpected); function test($theInput, $theExpected) { $theEncoded = base64_encode($theInput); $theDecoded = base64_decode($theEncoded); echo " Input : $theInput\n"; echo " Encoded : $theEncoded\n"; echo " Expected: $theExpected\n"; echo " Decoded : $theDecoded\n"; } ?>
Here is the test result:
herong> php Base64_Encode_Test.php Test 1: Input : A Encoded : QQ== Expected: QQ== Decoded : A Test 2: Input : AB Encoded : QUI= Expected: QUI= Decoded : AB Test 3: Input : ABC Encoded : QUJD Expected: QUJD Decoded : ABC Test 4: Input : The quick brown fox jumps over the lazy dog. Encoded : VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4= Expected: VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4= Decoded : The quick brown fox jumps over the lazy dog.
The result matches my expectation perfectly.
Table of Contents
►Base64 Encoding and Decoding Tools
Base64.Guru - Base64 Online Tool
Windows Command - "certutil -encode/-decode"
Java Built-In Implementation of Base64
Java Built-In Implementation of MIME Base64
Python Built-In Implementation of Base64
Python Built-In Implementation of MIME Base64
►PHP Built-In Implementation of Base64
PHP Built-In Implementation of MIME Base64
Perl Built-In Implementation of Base64
Perl Built-In Implementation of MIME Base64
Base64URL - URL Safe Base64 Encoding