W3docs

quoted_printable_decode()

Our article is about the PHP function quoted_printable_decode(), which is used to decode a quoted-printable string. This function is useful for working with

The PHP quoted_printable_decode() function converts a quoted-printable string back into its original 8-bit form. Quoted-printable is an encoding defined by MIME (RFC 2045) that lets text containing non-ASCII bytes travel safely through systems — chiefly email — that were designed to handle only plain 7-bit ASCII. This page explains what the encoding looks like, when you actually need to decode it, and the gotchas to watch for.

What is quoted-printable?

In quoted-printable encoding, any byte that is not a "safe" printable ASCII character is written as an equals sign followed by its two-digit hexadecimal value. For example, a space can appear as =20 and the accented letter è (UTF-8 byte sequence C3 A8) becomes =C3=A8. A lone = at the end of a line is a soft line break used to keep encoded lines under 76 characters; it is removed on decode.

You will most often meet this encoding in the body and headers of email messages, where the Content-Transfer-Encoding: quoted-printable header tells the client how the text was stored.

Syntax

quoted_printable_decode(string $string): string

It takes a single argument and returns the decoded string.

ParameterTypeDescription
$stringstringThe quoted-printable string to decode.

Return value: the decoded 8-bit string. Sequences that are not valid quoted-printable are returned unchanged rather than raising an error.

Basic example

php— editable, runs on the server

Here =20 is the hexadecimal code for a space (ASCII 32), so the output is:

Hello World!

Decoding accented text and soft line breaks

The function shines on real-world content that mixes encoded multibyte characters with the soft line breaks (= at end of line) used to wrap long lines:

<?php
$encoded = "J=27interpr=C3=A8te=20du=20fran=C3=A7ais,=\n et c=27est tout.";
echo quoted_printable_decode($encoded);
?>

Output:

J'interprète du français, et c'est tout.

Notice two things: each =XX pair (such as =C3=A8 for è) is turned back into its raw byte, and the trailing = followed by a newline is removed entirely, joining the two lines into one.

When would I use it?

Reach for quoted_printable_decode() when you are reading raw email — for instance parsing a message pulled from an IMAP mailbox whose part is marked Content-Transfer-Encoding: quoted-printable. It is the inverse of quoted_printable_encode(), which you call before sending such content.

A few things to keep in mind:

  • It does not change the character set. Decoding =C3=A8 gives you the raw UTF-8 bytes for è; the result is only correct if you treat the output as UTF-8. The function knows nothing about encodings — it just reverses the hex escaping.
  • Encoded-word headers are different. Subject lines like =?UTF-8?Q?...?= use a related but distinct format; decode those with mb_decode_mimeheader() or iconv_mime_decode() instead.
  • Invalid input is tolerated. A stray = not followed by valid hex is left as-is, so malformed data will not throw.

Practice

Practice
What does the quoted_printable_decode() function do in PHP?
What does the quoted_printable_decode() function do in PHP?
Was this page helpful?