Below is a list of libraries that support Base64 encoding and decoding in PHP along with sample code for each library.
1. PHP Standard Library (PHP 5.0 and above)
PHP provides the base64_encode
and base64_decode
functions in its standard library.
<?php
$originalString = "test input";
// Encode
$encodedString = base64_encode($originalString);
echo "Encoded: " . $encodedString . "\n";
// Decode
$decodedString = base64_decode($encodedString);
echo "Decoded: " . $decodedString . "\n";
?>
2. OpenSSL Extension
The OpenSSL library in PHP also supports Base64 encoding and decoding through the openssl_encrypt
and openssl_decrypt
functions.
<?php
$originalString = "test input";
// Encode
$encodedString = base64_encode(openssl_encrypt($originalString, 'AES-128-ECB', 'secret', 0, ''));
echo "Encoded: " . $encodedString . "\n";
// Decode
$decodedString = openssl_decrypt(base64_decode($encodedString), 'AES-128-ECB', 'secret', 0, '');
echo "Decoded: " . $decodedString . "\n";
?>
3. Sodium Extension (PHP 7.2 and above)
The Sodium library, starting from PHP 7.2, supports Base64 encoding and decoding.
<?php
$originalString = "test input";
// Encode
$encodedString = sodium_bin2base64($originalString, SODIUM_BASE64_VARIANT_ORIGINAL);
echo "Encoded: " . $encodedString . "\n";
// Decode
$decodedString = sodium_base642bin($encodedString, SODIUM_BASE64_VARIANT_ORIGINAL);
echo "Decoded: " . $decodedString . "\n";
?>
4. Laminas (Zend Framework)
Laminas provides utilities for Base64 encoding and decoding through its cryptographic components.
<?php
require 'vendor/autoload.php';
use Laminas\Crypt\BlockCipher;
use Laminas\Crypt\Symmetric\Mcrypt;
// Encode
$blockCipher = BlockCipher::factory('mcrypt', ['algo' => 'aes']);
$blockCipher->setKey('encryptionkey');
$originalString = "test input";
$encrypted = $blockCipher->encrypt($originalString);
$encodedString = base64_encode($encrypted);
echo "Encoded: " . $encodedString . "\n";
// Decode
$decodedString = $blockCipher->decrypt(base64_decode($encodedString));
echo "Decoded: " . $decodedString . "\n";
?>
Add the dependency to composer.json
:
{
"require": {
"laminas/laminas-crypt": "^3.4"
}
}
5. Firebase JWT Library
The Firebase JWT Library supports Base64 encoding and decoding through the encoding and decoding of JWT tokens.
<?php
require 'vendor/autoload.php';
use Firebase\JWT\JWT;
$originalString = "test input";
$key = "example_key";
// Encode
$encodedString = JWT::encode($originalString, $key);
echo "Encoded: " . $encodedString . "\n";
// Decode
$decodedString = JWT::decode($encodedString, $key, array('HS256'));
echo "Decoded: " . json_encode($decodedString) . "\n";
?>
Add the dependency to composer.json
:
{
"require": {
"firebase/php-jwt": "^6.0"
}
}
Welcome to our Php Encode Base64 Online tool, the ultimate solution to Php Encode Base64 Online quickly and easily. Whether you need to convert binary data for safe transmission or storage, our tool provides a simple and efficient way to encode your data to Base64 format.
Why Use Base64 Encoding?
Base64 encoding is a method of converting binary data into a text format using 64 characters. This encoding scheme is especially useful when you need to transfer or store data over media designed to handle text, such as email or JSON files. Base64 ensures your data remains intact and unmodified during transport.
Key Features of Our Base64 Encoding Tool
Simple and Fast Encoding
Our tool makes it incredibly easy to Php Encode Base64 Online. Simply enter your data, click the "Encode" button, and get your Base64 encoded result instantly. No complex configurations or technical knowledge required.
Secure and Private
We prioritize your privacy and data security. All communications with our servers are encrypted using SSL connections. We do not store or inspect the contents of your encoded data, ensuring complete confidentiality.
Free to Use
Our Base64 encoding tool is completely free. You don’t need to download any software or register for an account. Access our online tool anytime, anywhere, without any cost.
Advanced Options
Character Set Selection
While Base64 encoding does not inherently include character set information, our tool allows you to specify the character set used during encoding. Commonly, UTF-8 is used, but other options are available. If unsure, try the auto-detect option for the best results.
Live Encoding Mode
Enable live mode to see your data encoded in real-time using your browser’s built-in JavaScript functions. This feature supports the UTF-8 character set and does not send data to our servers, offering enhanced privacy.
How to Php Encode Base64 Online
Follow these simple steps to Php Encode Base64 Online:
- Enter your data:Paste your text or binary data into the input field.
- Click "Encode":Hit the "Encode" button to convert your data to Base64 format.
- Copy the result:Your encoded data will appear instantly. Copy it for use in your applications.
Example of Base64 Encoding
Here’s a quick example. Encoding the string "Hello, World!" in Base64 yields:
SGVsbG8sIFdvcmxkIQ==
In this example, the characters "H", "e", "l", "l", "o", ",", " ", "W", "o", "r", "l", "d", "!" are encoded into Base64 format, providing a safe and efficient way to transmit text data.