PHP's quoted_printable_encode in JavaScript

Here’s what our current JavaScript equivalent to PHP's quoted_printable_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
module.exports = function quoted_printable_encode (str) { // eslint-disable-line camelcase
// discuss at: http://locutus.io/php/quoted_printable_encode/
// original by: Theriault (https://github.com/Theriault)
// improved by: Brett Zamir (http://brett-zamir.me)
// improved by: Theriault (https://github.com/Theriault)
// example 1: quoted_printable_encode('a=b=c')
// returns 1: 'a=3Db=3Dc'
// example 2: quoted_printable_encode('abc \r\n123 \r\n')
// returns 2: 'abc =20\r\n123 =20\r\n'
// example 3: quoted_printable_encode('0123456789012345678901234567890123456789012345678901234567890123456789012345')
// returns 3: '012345678901234567890123456789012345678901234567890123456789012345678901234=\r\n5'
// test: skip-2
var hexChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
var RFC2045Encode1IN = / \r\n|\r\n|[^!-<>-~ ]/gm
var RFC2045Encode1OUT = function (sMatch) {
// Encode space before CRLF sequence to prevent spaces from being stripped
// Keep hard line breaks intact; CRLF sequences
if (sMatch.length > 1) {
return sMatch.replace(' ', '=20')
}
// Encode matching character
var chr = sMatch.charCodeAt(0)
return '=' + hexChars[((chr >>> 4) & 15)] + hexChars[(chr & 15)]
}
// Split lines to 75 characters; the reason it's 75 and not 76 is because softline breaks are
// preceeded by an equal sign; which would be the 76th character. However, if the last line/string
// was exactly 76 characters, then a softline would not be needed. PHP currently softbreaks
// anyway; so this function replicates PHP.
var RFC2045Encode2IN = /.{1,72}(?!\r\n)[^=]{0,3}/g
var RFC2045Encode2OUT = function (sMatch) {
if (sMatch.substr(sMatch.length - 2) === '\r\n') {
return sMatch
}
return sMatch + '=\r\n'
}
str = str
.replace(RFC2045Encode1IN, RFC2045Encode1OUT)
.replace(RFC2045Encode2IN, RFC2045Encode2OUT)
// Strip last softline break
return str.substr(0, str.length - 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/strings/quoted_printable_encode'). You could also require the strings module in full so that you could access strings.quoted_printable_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
1quoted_printable_encode('a=b=c')'a=3Db=3Dc'
2quoted_printable_encode('abc \r\n123 \r\n')'abc =20\r\n123 =20\r\n'
3quoted_printable_encode('0123456789012345678901234567890123456789012345678901234567890123456789012345')'012345678901234567890123456789012345678901234567890123456789012345678901234=\r\n5'

« More PHP strings functions