(PHP 5, PHP 7, PHP 8)
iconv_mime_decode_headers — Décode des en-têtes MIME multiples
$headers, int $mode = 0, ? string $encoding = null ): array |false
iconv_mime_decode_headers() décode les
en-têtes MIME multiples.
headersLes 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.
| 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é.
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.
| Version | Description |
|---|---|
| 8.0.0 |
encoding est désormais nullable.
|
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 ) )
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.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);
?>