PHP quoted_printable_encode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, quoted_printable_encode() converts an 8-bit string into quoted-printable text.

Special bytes become =XX codes, and long lines are wrapped. It is used when preparing email content.

Syntax

Call quoted_printable_encode() with the values you need. Start with this line.

PHP
$encoded = quoted_printable_encode("Café");
CallWhat it does
quoted_printable_encode("Café")Returns "Caf=C3=A9".
quoted_printable_encode("a=b")Returns "a=3Db".
quoted_printable_encode(str_repeat("a", 100))Adds soft line breaks to keep lines short.

Plain ASCII letters and digits stay as they are. Only special bytes are encoded.

Library declarations
PHP
quoted_printable_encode(string $string): string

Parameters

ParameterDescription
stringRequired. The string to encode.

Return Value

Return ValueDescription
stringThe quoted-printable encoded string.

PHP Examples

Example 1 — Encode an Accented Word

A non-ASCII letter is turned into =XX codes.

PHP
<?php
$text = "Café";

echo quoted_printable_encode($text), PHP_EOL;

How It Works

  1. Begin with UTF-8 text. The é uses two bytes.
  2. Call the function. Each special byte becomes =XX.
  3. Normal letters stay. Only é changes.
  4. Print the encoded string. You see Caf=C3=A9.

Example 2 — Equals Sign and Long Lines

The equals sign is encoded, and very long lines get soft breaks.

PHP
<?php
echo quoted_printable_encode("a=b"), PHP_EOL;

$long = quoted_printable_encode(str_repeat("a", 100));
echo str_contains($long, "=\r\n") ? "Soft line break added" : "No break", PHP_EOL;

How It Works

  1. Encode a real equals sign. It becomes =3D.
  2. Make a 100 character line. It is longer than the 76 limit.
  3. PHP wraps it. A soft break (= plus CRLF) is inserted.
  4. Check with str_contains. The break is found in the result.

Example 3 — Round Trip with Decode

Encoding and then decoding gives back the original text.

PHP
<?php
$text = "Café costs 5=5";

$encoded = quoted_printable_encode($text);
$decoded = quoted_printable_decode($encoded);

echo $encoded, PHP_EOL;
echo $decoded === $text ? "Round trip OK" : "Mismatch", PHP_EOL;

How It Works

  1. Encode the text. é and = both become codes.
  2. Decode it again. The codes turn back into bytes.
  3. Compare with ===. The strings are identical.
  4. This pair is lossless. No data is lost along the way.

Frequently Asked Questions

It converts a string to quoted-printable format, writing special bytes as =XX hex codes and wrapping long lines with soft line breaks.
string quoted_printable_encode(string $string). It returns the encoded string.
The equals sign starts every code in this format, so a real one must be escaped as =3D.
Lines are limited to 76 characters. Longer lines are wrapped using a trailing = and a CRLF line break.
Quoted-printable keeps mostly-ASCII text readable. Base64 is better for binary data like images.

Next: quotemeta()

quotemeta() adds a backslash before special regular-expression-style characters in a strin.

quotemeta() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful