Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { quoted_printable_encode } from 'locutus/php/strings/quoted_printable_encode'.
Or with CommonJS: const { quoted_printable_encode } = require('locutus/php/strings/quoted_printable_encode')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
const hexChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'] constRFC2045Encode1IN = / \r\n|\r\n|[^!-<>-~ ]/gm constRFC2045Encode1OUT = function (sMatch: string): string { // 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 const 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.
// Strip last softline break (only if it's a soft break =\r\n, not a hard break \r\n) if (str.endsWith('=\r\n')) { return str.slice(0, -3) } return str }
const hexChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'] constRFC2045Encode1IN = / \r\n|\r\n|[^!-<>-~ ]/gm constRFC2045Encode1OUT = 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 const 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.
constRFC2045Encode2IN = /.{1,72}(?!\r\n)[^=]{0,3}/g constRFC2045Encode2OUT = function (sMatch) { if (sMatch.endsWith('\r\n')) { return sMatch } return sMatch + '=\r\n' }
// Strip last softline break (only if it's a soft break =\r\n, not a hard break \r\n) if (str.endsWith('=\r\n')) { return str.slice(0, -3) } return str }
Improve this function
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.