Fork me on GitHub

Learn how to do it in JavaScript. Explore boundaries porting languages. Enjoy functions that turn out to be useful.

Here’s what a JavaScript equivalent for PHP’s quoted_printable_encode looks like.

function quoted_printable_encode (str) {
  //  discuss at: http://phpjs.org/functions/quoted_printable_encode/
  // original by: Theriault
  // improved by: Brett Zamir (http://brett-zamir.me)
  // improved by: 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'

  var hexChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'],
    RFC2045Encode1IN = / \r\n|\r\n|[^!-<>-~ ]/gm,
    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.
  RFC2045Encode2IN = /.{1,72}(?!\r\n)[^=]{0,3}/g,
    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

A community initiative

php.js offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that aren't perfect, in the hopes to spark better contributions. Do you have one? You can directly edit this file on GitHub, this will create a Pull Request that the core team can review, and merge into our codebase.

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

Example 1

quoted_printable_encode('a=b=c');
Should return:
'a=3Db=3Dc'

Example 2

quoted_printable_encode('abc   \r\n123   \r\n');
Should return:
'abc  =20\r\n123  =20\r\n'

Example 3

quoted_printable_encode('0123456789012345678901234567890123456789012345678901234567890123456789012345');
Should return:
'012345678901234567890123456789012345678901234567890123456789012345678901234=\r\n5'

« More strings functions