PHP Classes

Oire Base64: URL safe encoding and decoding of data with Base64

Recommend this page to a friend!
  Info   View files Documentation   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 65%Total: 154 This week: 1All time: 9,033 This week: 571Up
Version License PHP version Categories
oirebase64 1.0MIT/X Consortium ...5PHP 5, Text processing
Description 

Author

This class can perform URL safe encoding and decoding of data with Base64.

It can take a string and encode it using Base64 but replaces = with ~ to make the encoded data safe for putting in a URL.

The class can also decodes previously encoded base64 data using this class.

Innovation Award
PHP Programming Innovation award nominee
April 2017
Number 13
Base64 is a well-known algorithm that is used to encode data in a way that it is represented only by ASCII characters.

Base64 can be used for instance to pass values in URLs. However, the encoded values may still need to be encoded again using the PHP URLEncode function because base64 returns = characters and the = character is used as separator between parameters names and values passed in the URL.

This simple class provides a variant of the Base64 algorithm that uses a replacement character for = using ~ instead, so it is not necessary to use URLEncode to use the returned values in URLs.

Manuel Lemos
Picture of Andre Polykanine A.K.A. Menelion Elensúlë
  Performance   Level  
Name: Andre Polykanine A.K.A. ... is available for providing paid consulting. Contact Andre Polykanine A.K.A. ... .
Classes: 3 packages by
Country: Ukraine Ukraine
Age: ???
All time rank: 213434 in Ukraine Ukraine
Week rank: 420 Up7 in Ukraine Ukraine Up
Innovation award
Innovation award
Nominee: 1x

Documentation

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.


  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (1 file)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file autoload.php Aux. Auxiliary script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file base64.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file Base64Test.php Class Class source

 Version Control Reuses Unique User Downloads Download Rankings  
 100%2
Total:154
This week:1
All time:9,033
This week:571Up
User Ratings User Comments (1)
 All time
Utility:93%StarStarStarStarStar
Consistency:93%StarStarStarStarStar
Documentation:87%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:65%StarStarStarStar
Rank:649
 
nice
6 years ago (muabshir)
70%StarStarStarStar