iconv_mime_decode_headers

(PHP 5, PHP 7, PHP 8)

iconv_mime_decode_headers — DĂ©code des en-tĂȘtes MIME multiples

Description

function iconv_mime_decode_headers(string $headers, int $mode = 0, ?string $encoding = null): array|false

iconv_mime_decode_headers() dĂ©code les en-tĂȘtes MIME multiples.

Liste de paramĂštres

headers

Les en-tĂȘtes encodĂ©s, sous la forme d'une chaĂźne de caractĂšres.

mode

mode dĂ©termine le comportement de la fonction, si iconv_mime_decode_headers() rencontre un en-tĂȘte MIME malformĂ©. Il est possible de spĂ©cifier toute combinaison des masques suivants.

Masques acceptés par la fonction iconv_mime_decode_headers()
Valeur Constante Description
1 ICONV_MIME_DECODE_STRICT Si activĂ©e, l'en-tĂȘte fourni est dĂ©codĂ© en respectant scrupuleusement le standard dĂ©fini dans la » RFC2047. Cette option est dĂ©sactivĂ©e par dĂ©faut, car il existe de nombreux clients mail dĂ©fectueux qui ne suivent pas la spĂ©cification et qui ne produisent pas d'en-tĂȘtes MIME corrects.
2 ICONV_MIME_DECODE_CONTINUE_ON_ERROR Si cette option est activĂ©e, iconv_mime_decode_headers() tente d'ignorer les erreurs de syntaxe et continue de traiter l'en-tĂȘte donnĂ©.
encoding

Le paramÚtre optionnel encoding spécifie le jeu de caractÚres utilisé pour représenter le résultat. S'il est omis ou null, iconv.internal_encoding sera utilisé.

Valeurs de retour

Retourne un tableau associatif qui contient l'ensemble des en-tĂȘtes MIME spĂ©cifiĂ©s par le paramĂštre headers en cas de succĂšs, ou bien false si une erreur survient durant le dĂ©codage.

Chaque clĂ© du tableau retournĂ© contient un nom d'en-tĂȘte distinct, et sa valeur correspondante. Si plusieurs champs ont le mĂȘme nom, iconv_mime_decode_headers() fera de ce champ un tableau indexĂ©, avec les valeurs dans leur ordre d'apparence. Il est Ă  noter que les noms d'en-tĂȘtes ne sont pas insensibles Ă  la casse.

Historique

Version Description
8.0.0 encoding est désormais nullable.

Exemples

Exemple #1 Exemple avec iconv_mime_decode_headers()

<?php
$headers_string
= <<<EOF
Subject: =?UTF-8?B?UHLDvGZ1bmcgUHLDvGZ1bmc=?=
To: example@example.com
Date: Thu, 1 Jan 1970 00:00:00 +0000
Message-Id: <example@example.com>
Received: from localhost (localhost [127.0.0.1]) by localhost
with SMTP id example for <example@example.com>;
Thu, 1 Jan 1970 00:00:00 +0000 (UTC)
(envelope-from example-return-0000-example=example.com@example.com)
Received: (qmail 0 invoked by uid 65534); 1 Thu 2003 00:00:00 +0000

EOF;

$headers = iconv_mime_decode_headers($headers_string, 0, "ISO-8859-1");
print_r($headers);
?>

L'exemple ci-dessus va afficher :

Array
(
    [Subject] => PrĂŒfung PrĂŒfung
    [To] => example@example.com
    [Date] => Thu, 1 Jan 1970 00:00:00 +0000
    [Message-Id] => <example@example.com>
    [Received] => Array
        (
            [0] => from localhost (localhost [127.0.0.1]) by localhost with SMTP id example for <example@example.com>; Thu, 1 Jan 1970 00:00:00 +0000 (UTC) (envelope-from example-return-0000-example=example.com@example.com)
            [1] => (qmail 0 invoked by uid 65534); 1 Thu 2003 00:00:00 +0000
        )

)

Voir aussi

add a note

User Contributed Notes 2 notes

up
0
phpmanual at NOSPAM dot headbank dot co dot uk ¶
1 year ago
Just in case this catches anyone else: If your headers string has any leading linebreaks, this function will reject it and return an empty array. If that might apply to your input, sanitise it with ltrim().

Trailing empty lines are tolerated/ignored.

Other quirks I noticed just now:

1. Leading whitespace (" " or "\t") in the *first* line will be included in the header's key name in the returned array. ltrim() will prevent that too.

2. Leading whitespace in any subsequent header (before the key) will cause that line to be appended to the preceding header's value, as though it were a run-on of that header.
up
0
TheConstructor ¶
15 years ago
If you need lower-case header-names (as I read the documentation case is not guranteed) try something like

<?php

$headers_string = <<<EOF
Subject: =?UTF-8?B?UHLDvGZ1bmcgUHLDvGZ1bmc=?=
To: example@example.com
Date: Thu, 1 Jan 1970 00:00:00 +0000
Message-Id: <example@example.com>
Received: from localhost (localhost [127.0.0.1]) by localhost
    with SMTP id example for <example@example.com>;
    Thu, 1 Jan 1970 00:00:00 +0000 (UTC)
    (envelope-from example-return-0000-example=example.com@example.com)
Received: (qmail 0 invoked by uid 65534); 1 Thu 2003 00:00:00 +0000

EOF;

$headers =  iconv_mime_decode_headers($headers_string, 0, "ISO-8859-1");

$headers = array_combine(array_map("strtolower", array_keys($headers)), array_values($headers));

print_r($headers);
?>