Install via yarn add locutus and import:
import { base64_decode } from 'locutus/php/url/base64_decode'.
Or with CommonJS: const { base64_decode } = require('locutus/php/url/base64_decode')
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_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==')
'Kevin van Zonneveld'
2
base64_decode('YQ==')
'a'
3
base64_decode('4pyTIMOgIGxhIG1vZGU=')
'✓ à la mode'
Dependencies
This function uses the following Locutus functions:
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 dec = '' consttmpArr: string[] = []
if (!encodedData) { return encodedData }
encodedData += ''
do { // unpack four hexets into three octets using index points in b64 h1 = b64.indexOf(encodedData.charAt(i++)) h2 = b64.indexOf(encodedData.charAt(i++)) h3 = b64.indexOf(encodedData.charAt(i++)) h4 = b64.indexOf(encodedData.charAt(i++))
const atobValue = getPhpGlobalCallable('atob') if (atobValue) { const decoded = atobValue(String(encodedData)) if (typeof decoded === 'string') { returndecodeUTF8string(decoded) } }
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 dec = '' const tmpArr = []
if (!encodedData) { return encodedData }
encodedData += ''
do { // unpack four hexets into three octets using index points in b64 h1 = b64.indexOf(encodedData.charAt(i++)) h2 = b64.indexOf(encodedData.charAt(i++)) h3 = b64.indexOf(encodedData.charAt(i++)) h4 = b64.indexOf(encodedData.charAt(i++))
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' }
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 dec = '' consttmpArr: string[] = []
if (!encodedData) { return encodedData }
encodedData += ''
do { // unpack four hexets into three octets using index points in b64 h1 = b64.indexOf(encodedData.charAt(i++)) h2 = b64.indexOf(encodedData.charAt(i++)) h3 = b64.indexOf(encodedData.charAt(i++)) h4 = b64.indexOf(encodedData.charAt(i++))
const atobValue = getPhpGlobalCallable('atob') if (atobValue) { const decoded = atobValue(String(encodedData)) if (typeof decoded === 'string') { returndecodeUTF8string(decoded) } }
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 dec = '' const tmpArr = []
if (!encodedData) { return encodedData }
encodedData += ''
do { // unpack four hexets into three octets using index points in b64 h1 = b64.indexOf(encodedData.charAt(i++)) h2 = b64.indexOf(encodedData.charAt(i++)) h3 = b64.indexOf(encodedData.charAt(i++)) h4 = b64.indexOf(encodedData.charAt(i++))
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.