With the Base64 codec you will be able to encode into and decode from Base64 data in an object oriented way. It wraps PHP's functions base64_encode()
and base64_decode()
.
Install the latest version with
$ composer require codekandis/base64-codec
The following examples show how to encode a value.
$value = '8ÇÂ<Ç<ï¯ñ×78B>ïAË¡4Wïc§ÿPîXø4\Êá¡t7?/SµÚ²·x}0¤¯ç»M';
( new Base64Encoder() )
->encodeToStandard( $value );
/**
* OMOHw4I8w4c8w6/Cr8Oxw5c3OEI+w69Bw4vCoTRXw69jwqfDv1DDrljDuDRcw4rDocKhdDc/L1PCtcOawrLCt3h9MMKkwq/Dp8K7TQ==
*/
( new Base64Encoder() )
->encodeToUriSafe( $value );
/**
* OMOHw4I8w4c8w6_Cr8Oxw5c3OEI-w69Bw4vCoTRXw69jwqfDv1DDrljDuDRcw4rDocKhdDc_L1PCtcOawrLCt3h9MMKkwq_Dp8K7TQ
*/
The following examples show how to decode a value.
$standardBase64Value = 'OMOHw4I8w4c8w6/Cr8Oxw5c3OEI+w69Bw4vCoTRXw69jwqfDv1DDrljDuDRcw4rDocKhdDc/L1PCtcOawrLCt3h9MMKkwq/Dp8K7TQ==';
( new Base64Decoder() )
->decodeFromStandard( $standardBase64Value );
/**
* 8ÇÂ<Ç<ï¯ñ×78B>ïAË¡4Wïc§ÿPîXø4\Êá¡t7?/SµÚ²·x}0¤¯ç»M
*/
$uriSafeBase64Value = 'OMOHw4I8w4c8w6_Cr8Oxw5c3OEI-w69Bw4vCoTRXw69jwqfDv1DDrljDuDRcw4rDocKhdDc_L1PCtcOawrLCt3h9MMKkwq_Dp8K7TQ';
( new Base64Decoder() )
->decodeFromUriSafe( $uriSafeBase64Value );
/**
* 8ÇÂ<Ç<ï¯ñ×78B>ïAË¡4Wïc§ÿPîXø4\Êá¡t7?/SµÚ²·x}0¤¯ç»M
*/
base64_decode()
accepts an additional argument $strict
to specify if it's forced to decode into an associative array. This argument is omitted in the Base64DecoderInterface
while it's intended to always decode in strict mode.