PHP Classes

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

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-05-07 (10 days ago) RSS 2.0 feedStarStarStarStar 65%Total: 154 All time: 9,043 This week: 206Up
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. ... <contact>
Classes: 3 packages by
Country: Ukraine Ukraine
Innovation award
Innovation award
Nominee: 1x

Documentation

Oirë Base64, URL-safe Base64 Handling

Note! This library is not maintained anymore.

Please use Iridium security library instead.

Latest Version on Packagist MIT License

Encodes data to Base64 URL-safe way and decodes encoded data.

Requirements

This library requires PHP 7.3 or above.

Installation

Install via Composer:

composer require oire/base64

Running Tests

Run ./vendor/bin/phpunit in the project directory.

Compatibility with Earlier Versions of PHP

If you want a version compatible with PHP 7.1.2, please install version 1 instead:

composer require "oire/base64 ^1"

Usage Examples

use Oire\Base64\Base64;
use Oire\Base64\Exception\Base64Exception;

$text = "The quick brown fox jumps over the lazy dog";
$encoded = Base64::encode($text);
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.:

$encoded = Base64::encode($text, true);
echo $encoded.PHP_EOL;

This will output:

VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw~~

To decode the data, simply call Base64::decode():

$encoded = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw";

try {
    $decoded = Base64::decode($encoded);
} catch(Base64Exception $e) {
    // Handle errors
}

echo $decoded.PHP_EOL;

This will output:

The quick brown fox jumps over the lazy dog

License

Copyright © 2017-2021, Andre Polykanine also known as Menelion Elensúlë, The Magical Kingdom of Oirë. This software is licensed under an MIT license.


  Files folder image Files (14)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (1 file, 1 directory)
Files folder imagetests (1 file)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .php_cs.dist Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file psalm.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (14)  /  .github  
File Role Description
Files folder imageworkflows (3 files)

  Files folder image Files (14)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file php-cs-fixer.yml Data Auxiliary data
  Accessible without login Plain text file psalm.yml Data Auxiliary data
  Accessible without login Plain text file run-tests.yml Data Auxiliary data

  Files folder image Files (14)  /  src  
File Role Description
Files folder imageException (1 file)
  Plain text file Base64.php Class Class source

  Files folder image Files (14)  /  src  /  Exception  
File Role Description
  Plain text file Base64Exception.php Class Class source

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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Reuses Unique User Downloads Download Rankings  
 100%2
Total:154
This week:0
All time:9,043
This week:206Up
User Ratings User Comments (1)
 All time
Utility:93%StarStarStarStarStar
Consistency:93%StarStarStarStarStar
Documentation:87%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:65%StarStarStarStar
Rank:653
 
nice
8 years ago (muabshir)
70%StarStarStarStar