PHP – How to decode a MIME header field using iconv_mime_decode() function?


In PHP, iconv_mime_decode() function is used to decode a MIME header field. This is an inbuilt function in PHP that is used from the PHP 5 version.

Syntax

String iconv_mime_decode(string $string, int $mode, string $encoding)

Parameters

The iconv_mime_decode() accepts three different parameters− $string, $mode and $encoding. $string and $mode are mandatory parameters, but $encoding is optional.

  • $string − The $string parameter is used for the encoded header. It is a string-type parameter.

  • $mode − The $mode parameter determines the behavior in the event iconv_mime_decode() it encounters the malformed MIME header field. We can specify any combination of the below-given bitmasks.

    List of bitmasks acceptable to iconv_mime_decode_headers()

    • ICONV_MIME_DECODE_STRICT

    • ICONV_MIME_DECODE_CONTINUE_ON_ERROR

    • ICONV_MIME_DECODE_STRICT − If the iconv_mime_decode_strict is set, the given header is decoded in full conformance but this option is disabled by default due to a lot of broken mail user agents that do not follow the requirement and do not produce the correct MIME header.

    • ICONV_MIME_DECODE_CONTINUE_ON_ERROR − If the iconv_mime_decode_continue_on_error() parameter is set, it tries to ignore any grammatical errors and continues to process a given header.

  • $encoding − The encoding is an optional parameter that is used to specifies the character set to represent the result. The iconv.internal_encoding will be used if omitted or null.

Return Values

The iconv_mime_decode() function returns a decoded MIME field on success or it returns False if any error arises during the decoding.

Example

 Live Demo

<?php
   // This yields "Sub: Preços Olà.txt"
   echo iconv_mime_decode("Sub: =?utf-8?B?UHJlw4PCp29zIE9sw4PCoA==?=.txt ",
   0, "ISO-8859-1");
?>

Output

Sub: Preços Olà.txt

Updated on: 21-Aug-2021

302 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements