Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { base64_encode } from 'locutus/php/url/base64_encode'.
Or with CommonJS: const { base64_encode } = require('locutus/php/url/base64_encode')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
base64_encode('Kevin van Zonneveld')
'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
2
base64_encode('a')
'YQ=='
3
base64_encode('✓ à la mode')
'4pyTIMOgIGxhIG1vZGU='
Dependencies
This function uses the following Locutus functions:
exportfunctionbase64_encode(stringToEncode: string | null | undefined): string | null | undefined { // discuss at: https://locutus.io/php/base64_encode/ // parity verified: PHP 8.3 // original by: Tyler Akins (https://rumkin.com) // improved by: Bayron Guevara // improved by: Thunder.m // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Rafał Kukawski (https://blog.kukawski.pl) // bugfixed by: Pellentesque Malesuada // improved by: Indigo744 // example 1: base64_encode('Kevin van Zonneveld') // returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA==' // example 2: base64_encode('a') // returns 2: 'YQ==' // example 3: base64_encode('✓ à la mode') // returns 3: '4pyTIMOgIGxhIG1vZGU='
// encodeUTF8string() // Internal function to encode properly UTF8 string // Adapted from Solution #1 at https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding const encodeUTF8string = function (str: string): string { // first we use encodeURIComponent to get percent-encoded UTF-8, // then we convert the percent encodings into raw bytes which // can be fed into the base64 encoding algorithm. returnencodeURIComponent(str).replace(/%([0-9A-F]{2})/g, functiontoSolidBytes(_match, p1: string) { returnString.fromCharCode(Number.parseInt(p1, 16)) }) }
const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' leto1: number leto2: number leto3: number leth1: number leth2: number leth3: number leth4: number letbits: number let i = 0 let ac = 0 let enc = '' consttmpArr: string[] = []
if (!stringToEncode) { return stringToEncode }
stringToEncode = encodeUTF8string(stringToEncode)
do { // pack three octets into four hexets o1 = stringToEncode.charCodeAt(i++) o2 = stringToEncode.charCodeAt(i++) o3 = stringToEncode.charCodeAt(i++)
// use hexets to index into b64, and append result to encoded string tmpArr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4) } while (i < stringToEncode.length)
exportfunctionbase64_encode(stringToEncode) { // discuss at: https://locutus.io/php/base64_encode/ // parity verified: PHP 8.3 // original by: Tyler Akins (https://rumkin.com) // improved by: Bayron Guevara // improved by: Thunder.m // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Rafał Kukawski (https://blog.kukawski.pl) // bugfixed by: Pellentesque Malesuada // improved by: Indigo744 // example 1: base64_encode('Kevin van Zonneveld') // returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA==' // example 2: base64_encode('a') // returns 2: 'YQ==' // example 3: base64_encode('✓ à la mode') // returns 3: '4pyTIMOgIGxhIG1vZGU='
// encodeUTF8string() // Internal function to encode properly UTF8 string // Adapted from Solution #1 at https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding const encodeUTF8string = function (str) { // first we use encodeURIComponent to get percent-encoded UTF-8, // then we convert the percent encodings into raw bytes which // can be fed into the base64 encoding algorithm. returnencodeURIComponent(str).replace(/%([0-9A-F]{2})/g, functiontoSolidBytes(_match, p1) { returnString.fromCharCode(Number.parseInt(p1, 16)) }) }
const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' let o1 let o2 let o3 let h1 let h2 let h3 let h4 let bits let i = 0 let ac = 0 let enc = '' const tmpArr = []
if (!stringToEncode) { return stringToEncode }
stringToEncode = encodeUTF8string(stringToEncode)
do { // pack three octets into four hexets o1 = stringToEncode.charCodeAt(i++) o2 = stringToEncode.charCodeAt(i++) o3 = stringToEncode.charCodeAt(i++)
// use hexets to index into b64, and append result to encoded string tmpArr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4) } while (i < stringToEncode.length)
functionisObjectLike(value: PhpInput): value is PhpArrayLike<PhpInput> { returntypeof value === 'object' && value !== null }
function isPhpCallable<TArgsextendsPhpCallableArgs = PhpCallableArgs, TResult = PhpInput>( value: PhpInput, ): value is PhpCallable<TArgs, TResult> { returntypeof value === 'function' }
// php/url/base64_encode (target function module) functionbase64_encode(stringToEncode: string | null | undefined): string | null | undefined { // discuss at: https://locutus.io/php/base64_encode/ // parity verified: PHP 8.3 // original by: Tyler Akins (https://rumkin.com) // improved by: Bayron Guevara // improved by: Thunder.m // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Rafał Kukawski (https://blog.kukawski.pl) // bugfixed by: Pellentesque Malesuada // improved by: Indigo744 // example 1: base64_encode('Kevin van Zonneveld') // returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA==' // example 2: base64_encode('a') // returns 2: 'YQ==' // example 3: base64_encode('✓ à la mode') // returns 3: '4pyTIMOgIGxhIG1vZGU='
// encodeUTF8string() // Internal function to encode properly UTF8 string // Adapted from Solution #1 at https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding const encodeUTF8string = function (str: string): string { // first we use encodeURIComponent to get percent-encoded UTF-8, // then we convert the percent encodings into raw bytes which // can be fed into the base64 encoding algorithm. returnencodeURIComponent(str).replace(/%([0-9A-F]{2})/g, functiontoSolidBytes(_match, p1: string) { returnString.fromCharCode(Number.parseInt(p1, 16)) }) }
const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' leto1: number leto2: number leto3: number leth1: number leth2: number leth3: number leth4: number letbits: number let i = 0 let ac = 0 let enc = '' consttmpArr: string[] = []
if (!stringToEncode) { return stringToEncode }
stringToEncode = encodeUTF8string(stringToEncode)
do { // pack three octets into four hexets o1 = stringToEncode.charCodeAt(i++) o2 = stringToEncode.charCodeAt(i++) o3 = stringToEncode.charCodeAt(i++)
// use hexets to index into b64, and append result to encoded string tmpArr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4) } while (i < stringToEncode.length)
functiongetPhpGlobalEntry(key) { const value = globalContext[key] returntypeof value === 'undefined' ? undefined : value }
functiongetPhpGlobalCallable(key) { const value = getPhpGlobalEntry(key) returnisPhpCallable(value) ? value : undefined }
functiongetPhpObjectEntry(value, key) { if ((typeof value !== 'object' && typeof value !== 'function') || value === null) { returnundefined }
let current = value while (current) { const descriptor = Object.getOwnPropertyDescriptor(current, key) if (descriptor) { if (typeof descriptor.get === 'function') { const getterValue = descriptor.get.call(value) returntypeof getterValue === 'undefined' ? undefined : getterValue } const directValue = descriptor.value returntypeof directValue === 'undefined' ? undefined : directValue } current = Object.getPrototypeOf(current) }
returnundefined }
// php/url/base64_encode (target function module) functionbase64_encode(stringToEncode) { // discuss at: https://locutus.io/php/base64_encode/ // parity verified: PHP 8.3 // original by: Tyler Akins (https://rumkin.com) // improved by: Bayron Guevara // improved by: Thunder.m // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Rafał Kukawski (https://blog.kukawski.pl) // bugfixed by: Pellentesque Malesuada // improved by: Indigo744 // example 1: base64_encode('Kevin van Zonneveld') // returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA==' // example 2: base64_encode('a') // returns 2: 'YQ==' // example 3: base64_encode('✓ à la mode') // returns 3: '4pyTIMOgIGxhIG1vZGU='
// encodeUTF8string() // Internal function to encode properly UTF8 string // Adapted from Solution #1 at https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding const encodeUTF8string = function (str) { // first we use encodeURIComponent to get percent-encoded UTF-8, // then we convert the percent encodings into raw bytes which // can be fed into the base64 encoding algorithm. returnencodeURIComponent(str).replace(/%([0-9A-F]{2})/g, functiontoSolidBytes(_match, p1) { returnString.fromCharCode(Number.parseInt(p1, 16)) }) }
const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' let o1 let o2 let o3 let h1 let h2 let h3 let h4 let bits let i = 0 let ac = 0 let enc = '' const tmpArr = []
if (!stringToEncode) { return stringToEncode }
stringToEncode = encodeUTF8string(stringToEncode)
do { // pack three octets into four hexets o1 = stringToEncode.charCodeAt(i++) o2 = stringToEncode.charCodeAt(i++) o3 = stringToEncode.charCodeAt(i++)
// use hexets to index into b64, and append result to encoded string tmpArr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4) } while (i < stringToEncode.length)
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.