PHP Classes
elePHPant
Icontem

File: README.md

Recommend this page to a friend!
  Classes of Andre Polykanine A.K.A. Menelion Elensúlë  >  Oire Base64  >  README.md  >  Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Oire Base64
URL safe encoding and decoding of data with Base64
Author: By
Last change: Updating Readme, added Try...catch
Date: 1 year ago
Size: 1,721 bytes
 

Contents

Class file image Download

URL-safe Base64 Handling

Build Status MIT License

Encodes data to Base64 URL-safe way and decodes encoded data. Note: This package requires PHP 7.1 or above.

use \Oire\Base64;
$text = "The quick brown fox jumps over the lazy dog";
try {
	$encoded = Base64::encode($text);
} catch(Exception $e) {
	// Handle errors
}
echo $encoded.PHP_EOL;

This will output: VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw

By default, the encode() method truncates padding = signs as PHP?s built-in decoder handles this correctly. However, if the second parameter is given and set to true, = signs will be replaced with tildes (~), i.e.:

try {
	$encoded = Base64::encode($text, true);
} catch(Exception $e) {
	// Handle errors
}
echo $encoded.PHP_EOL;

This will output: VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw~~

To decode the data, simply call decode():

$encoded = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw";
try {
	$decoded = Base64::decode($encoded);
} catch(Exception $e) {
	// Handle errors
}
echo $decoded.PHP_EOL;

This will output: The quick brown fox jumps over the lazy dog

Installing

Via Composer:

composer require oire/base64

or manually:

require_once("oire/base64.php");

Running Tests

Run phpunit in the projects directory.

License

Copyright © 2017, Andre Polykanine also known as Menelion Elensúlë. This software is licensed under an MIT license.