quoted_printable_encode()
Our article is about the PHP function quoted_printable_encode(), which is used to encode a string into a quoted-printable format. This function is useful for
The PHP quoted_printable_encode() function encodes an 8-bit string into a quoted-printable string, the MIME encoding defined in RFC 2045. Quoted-printable keeps printable ASCII text readable while safely transmitting bytes that older, 7-bit email systems could otherwise corrupt — non-printable and non-ASCII bytes are written as =XX, where XX is the byte's value in uppercase hexadecimal.
This page covers the syntax, how the encoding rules work, runnable examples (including non-ASCII and line-wrapping behavior), common use cases, and how to reverse the operation.
Syntax
quoted_printable_encode(string $string): string| Parameter | Description |
|---|---|
$string | The 8-bit string to encode. |
Return value: the quoted-printable-encoded version of $string.
How the encoding works
Three rules cover almost every case:
- Printable ASCII stays as-is — letters, digits, and most punctuation pass through unchanged, which is why quoted-printable text is still mostly human-readable.
- Other bytes become
=XX— any byte outside the safe range (control characters,=itself, and every byte above 126, including UTF-8 bytes of accented or non-Latin characters) is written as an equals sign followed by two hex digits. - Long lines are soft-wrapped — lines are kept at or below 76 characters by inserting a "soft line break": a trailing
=immediately followed by a newline. The decoder removes it, so no real content is lost.
Basic example
Output:
Hello World!Because 'Hello World!' is entirely printable ASCII, the output is identical to the input — every character falls under the first rule.
Encoding non-ASCII text
The encoding only changes visibly once the string contains bytes that are unsafe for 7-bit transport, such as accented letters or symbols:
<?php
$string = 'Café costs £5';
echo quoted_printable_encode($string);
?>Output:
Caf=C3=A9 costs =C2=A35Here é is the two UTF-8 bytes 0xC3 0xA9, encoded as =C3=A9, and £ is 0xC2 0xA3, encoded as =C2=A3. The plain ASCII characters around them are untouched.
Soft line breaks for long lines
When a line would exceed 76 characters, the function inserts a soft line break (= plus a newline) so the output stays within email-line limits:
<?php
$string = str_repeat('abcdefghij', 9) . 'END';
echo quoted_printable_encode($string);
?>Output:
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcde=
fghijabcdefghijENDThe trailing = before the newline marks the break; a quoted-printable decoder reassembles the original single line.
When to use it
Reach for quoted_printable_encode() when you build the body or headers of an email by hand and need a 7-bit-safe representation of mostly-text content — for example, setting the Content-Transfer-Encoding: quoted-printable part of a MIME message. It is the right choice when the data is predominantly readable text with occasional special characters; for binary or heavily non-text data, base64 encoding is more compact.
In practice, most modern mail libraries (PHPMailer, Symfony Mailer) apply this encoding for you, so you rarely call it directly — but it is invaluable for debugging or for low-level scripts that assemble raw messages.
Reversing the encoding
Use quoted_printable_decode() to turn a quoted-printable string back into the original 8-bit data:
<?php
$encoded = quoted_printable_encode('Café costs £5');
echo quoted_printable_decode($encoded);
?>Output:
Café costs £5Related functions
quoted_printable_decode()— decode a quoted-printable string.utf8_encode()— convert an ISO-8859-1 string to UTF-8.utf8_decode()— convert a UTF-8 string to ISO-8859-1.