phpjs
Version:
94 lines (80 loc) • 3.53 kB
Markdown
---
layout: page
title: "JavaScript quoted_printable_decode function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/quoted_printable_decode:834
- /functions/view/quoted_printable_decode
- /functions/view/834
- /functions/quoted_printable_decode:834
- /functions/834
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's quoted_printable_decode
{% codeblock strings/quoted_printable_decode.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/quoted_printable_decode.js raw on github %}
function quoted_printable_decode (str) {
// From: http://phpjs.org/functions
// + original by: Ole Vrijenhoek
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + reimplemented by: Theriault
// + improved by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Theriault
// * example 1: quoted_printable_decode('a=3Db=3Dc');
// * returns 1: 'a=b=c'
// * example 2: quoted_printable_decode('abc =20\r\n123 =20\r\n');
// * returns 2: 'abc \r\n123 \r\n'
// * example 3: quoted_printable_decode('012345678901234567890123456789012345678901234567890123456789012345678901234=\r\n56789');
// * returns 3: '01234567890123456789012345678901234567890123456789012345678901234567890123456789'
// * example 4: quoted_printable_decode("Lorem ipsum dolor sit amet=23, consectetur adipisicing elit");
// * returns 4: 'Lorem ipsum dolor sit amet#, consectetur adipisicing elit'
// Removes softline breaks
var RFC2045Decode1 = /=\r\n/gm,
// Decodes all equal signs followed by two hex digits
RFC2045Decode2IN = /=([0-9A-F]{2})/gim,
// the RFC states against decoding lower case encodings, but following apparent PHP behavior
// RFC2045Decode2IN = /=([0-9A-F]{2})/gm,
RFC2045Decode2OUT = function (sMatch, sHex) {
return String.fromCharCode(parseInt(sHex, 16));
};
return str.replace(RFC2045Decode1, '').replace(RFC2045Decode2IN, RFC2045Decode2OUT);
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/quoted_printable_decode.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/strings/quoted_printable_decode.js)
### Example 1
This code
{% codeblock lang:js example %}
quoted_printable_decode('a=3Db=3Dc');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'a=b=c'
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
quoted_printable_decode('abc =20\r\n123 =20\r\n');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'abc \r\n123 \r\n'
{% endcodeblock %}
### Example 3
This code
{% codeblock lang:js example %}
quoted_printable_decode('012345678901234567890123456789012345678901234567890123456789012345678901234=\r\n56789');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'01234567890123456789012345678901234567890123456789012345678901234567890123456789'
{% endcodeblock %}
### Other PHP functions in the strings extension
{% render_partial _includes/custom/strings.html %}