oire / base64
Url-and filename-safe Base64 handling.
Installs: 2 527
Dependents: 3
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- oire/php-code-style: dev-master
- phpstan/phpstan: ^0.12.66
- phpunit/phpunit: ^9
- psalm/plugin-phpunit: ^0.15.0
- vimeo/psalm: ^4.4
This package is not auto-updated.
Last update: 2021-03-07 23:26:15 UTC
README
Note! This library is not maintained anymore.
Please use Iridium security library instead.
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.