PHP iconv_mime_decode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iconv

Definition

In PHP, iconv_mime_decode() decodes a MIME header field, such as an email subject that uses =?UTF-8?B?...?= words.

It returns the readable text in the character set you choose.

Syntax

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

PHP
$text = iconv_mime_decode($header, 0, "UTF-8");
CallWhat it does
iconv_mime_decode("Subject: =?UTF-8?Q?Caf=C3=A9?=", 0, "UTF-8")Returns "Subject: Café".
iconv_mime_decode("=?UTF-8?B?SGk=?=", 0, "UTF-8")Returns "Hi".

Encoded words use B for base64 or Q for quoted-printable. Both are supported.

Library declarations
PHP
iconv_mime_decode(string $string, int $mode = 0, ?string $encoding = null): string|false

Parameters

ParameterDescription
stringRequired. The header field to decode, with or without the field name.
modeOptional. A bit mask of ICONV_MIME_DECODE_STRICT and ICONV_MIME_DECODE_CONTINUE_ON_ERROR. Default is 0.
encodingOptional. The character set of the result. Default is the iconv internal encoding.

Return Value

Return ValueDescription
string|falseThe decoded text, or false on failure.

PHP Examples

Example 1 — Decode a Base64 Subject

Decode a header that uses the B (base64) scheme.

PHP
<?php
$header = "Subject: =?UTF-8?B?UHLDvGZ1bmcgUHLDvGZ1bmc=?=";

echo iconv_mime_decode($header, 0, "UTF-8"), PHP_EOL;

How It Works

  1. Start with an encoded header. The part inside =? ... ?= is the encoded word.
  2. Call iconv_mime_decode(). Pass UTF-8 as the output encoding.
  3. The encoded word is decoded. The field name stays as plain text.
  4. Print the result. You see Subject: Prüfung Prüfung.

Example 2 — Decode a Quoted-Printable Word

The Q scheme writes non-ASCII bytes as =XX.

PHP
<?php
$word = "=?UTF-8?Q?Pr=C3=BCfung?=";

echo iconv_mime_decode($word, 0, "UTF-8"), PHP_EOL;

How It Works

  1. Use the Q scheme. =C3=BC is the two bytes of the letter ü.
  2. Pass the word without a field name. That works too.
  3. Get the plain text. The =XX codes are joined.
  4. Pick the output charset. It tells PHP how to write the letter.

Example 3 — Decode Mixed Text

Plain text and an encoded word can sit side by side.

PHP
<?php
$mixed = "Hello =?UTF-8?Q?W=C3=B6rld?=";

echo iconv_mime_decode($mixed, 0, "UTF-8"), PHP_EOL;

How It Works

  1. Mix plain and encoded text. Hello is plain.
  2. Decode the string. Only the encoded word is changed.
  3. The result reads normally. You get Hello Wörld.
  4. Use it on real subjects. They often mix both forms.

Frequently Asked Questions

iconv_mime_decode() decodes a MIME header field with encoded words into readable text.
iconv_mime_decode(string $string, int $mode = 0, ?string $encoding = null): string|false.
B means base64 and Q means quoted-printable. The letter sits between the character set and the encoded text.
It controls how strict the parser is. ICONV_MIME_DECODE_CONTINUE_ON_ERROR lets decoding go on after a bad word.
Use iconv_mime_decode_headers(), which decodes a whole block of header lines at once.

Next: iconv_mime_decode_headers()

iconv_mime_decode_headers() decodes a block of MIME headers into an array.

iconv_mime_decode_headers() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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