JavaScript quoted_printable_decode
Convert a quoted-printable string to an 8 bit string
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 | function quoted_printable_decode (str) { // Convert a quoted-printable string to an 8 bit string // // version: 1008.1718 // discuss at: http://phpjs.org/functions/quoted_printable_decode // + 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); } |
Examples
» Example 1
Running
1 | quoted_printable_decode('a=3Db=3Dc'); |
Should return
1 | 'a=b=c' |
» Example 2
Running
1 | quoted_printable_decode('abc =20\r\n123 =20\r\n'); |
Should return
1 | 'abc \r\n123 \r\n' |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have quoted_printable_decode goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>