Skip to content
URL-safe Base64 handling in PHP 7.
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitignore
.travis.yml
LICENSE
README.md
autoload.php
composer.json
phpunit.xml

README.md

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.

You can’t perform that action at this time.