PHP's base64_encode in JavaScript

Here’s what our current JavaScript equivalent to PHP's base64_encode looks like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module.exports = function base64_encode (stringToEncode) { // eslint-disable-line camelcase
// discuss at: http://locutus.io/php/base64_encode/
// original by: Tyler Akins (http://rumkin.com)
// improved by: Bayron Guevara
// improved by: Thunder.m
// improved by: Kevin van Zonneveld (http://kvz.io)
// improved by: Kevin van Zonneveld (http://kvz.io)
// improved by: Rafał Kukawski (http://blog.kukawski.pl)
// bugfixed by: Pellentesque Malesuada
// 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='
if (typeof window !== 'undefined') {
if (typeof window.btoa !== 'undefined') {
return window.btoa(decodeURIComponent(encodeURIComponent(stringToEncode)))
}
} else {
return new Buffer(stringToEncode).toString('base64')
}
var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
var o1
var o2
var o3
var h1
var h2
var h3
var h4
var bits
var i = 0
var ac = 0
var enc = ''
var tmpArr = []
if (!stringToEncode) {
return stringToEncode
}
stringToEncode = decodeURIComponent(encodeURIComponent(stringToEncode))
do {
// pack three octets into four hexets
o1 = stringToEncode.charCodeAt(i++)
o2 = stringToEncode.charCodeAt(i++)
o3 = stringToEncode.charCodeAt(i++)
bits = o1 << 16 | o2 << 8 | o3
h1 = bits >> 18 & 0x3f
h2 = bits >> 12 & 0x3f
h3 = bits >> 6 & 0x3f
h4 = bits & 0x3f
// 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)
enc = tmpArr.join('')
var r = stringToEncode.length % 3
return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3)
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/url/base64_encode'). You could also require the url module in full so that you could access url.base64_encode instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1base64_encode('Kevin van Zonneveld')'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
2base64_encode('a')'YQ=='
3base64_encode('✓ à la mode')'4pyTIMOgIGxhIG1vZGU='

« More PHP url functions