imap_mime_header_decode

(PHP 4, PHP 5, PHP 7, PHP 8)

imap_mime_header_decodeDécode les éléments MIME d'un en-tête

Description

imap_mime_header_decode(string $string): array|false

Décode un message MIME qui contient des données non ASCII (voir » RFC2047).

Liste de paramètres

string

Le texte MIME

Valeurs de retour

Les éléments décodés sont retournés dans un tableau d'objets. Chacun de ces objets a deux propriétés : charset et text.

Si l'élément n'a pas été encodé, ou, en d'autres termes, s'il est en clair (plain US_ASCII), la propriété charset est mise à default.

Cette fonction retourne false en cas d'échec.

Exemples

Exemple #1 Exemple avec imap_mime_header_decode()

<?php
$text
= "=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@example.com>";

$elements = imap_mime_header_decode($text);
for (
$i=0; $i<count($elements); $i++) {
echo
"Charset : {$elements[$i]->charset}\n";
echo
"Texte : {$elements[$i]->text}\n\n";
}
?>

L'exemple ci-dessus va afficher :

Charset: ISO-8859-1
Text: Keld Jørn Simonsen

Charset: default
Text:  <keld@example.com>

Dans l'exemple ci-dessus, on trouve deux éléments : le premier a été encodé en ISO-8859-1, et le second est en clair.

Voir aussi

  • imap_utf8() - Convertit du texte au format MIME en UTF-8

add a note

User Contributed Notes 1 note

up
9
Anonymous
12 years ago
A nice way to decode strings is to use the mb_list_encodings(), but I had two problems with it: sometimes, the charset is uppercase in the header an lowercase in mb_list_encodings() and sometimes, the charset is not in the mb_list_encodings() list.<?phpfunction upperListEncode() { //convert mb_list_encodings() to uppercase    $encodes=mb_list_encodings();    foreach ($encodes as $encode) $tencode[]=strtoupper($encode);    return $tencode;    }function decode($string) {    $tabChaine=imap_mime_header_decode($string);    $texte='';    for ($i=0; $i<count($tabChaine); $i++) {                switch (strtoupper($tabChaine[$i]->charset)) { //convert charset to uppercase            case 'UTF-8': $texte.= $tabChaine[$i]->text; //utf8 is ok                break;            case 'DEFAULT': $texte.= $tabChaine[$i]->text; //no convert                break;            default: if (in_array(strtoupper($tabChaine[$i]->charset),upperListEncode())) //found in mb_list_encodings()                        {$texte.= mb_convert_encoding($tabChaine[$i]->text,'UTF-8',$tabChaine[$i]->charset);}                     else { //try to convert with iconv()                          $ret = iconv($tabChaine[$i]->charset, "UTF-8", $tabChaine[$i]->text);                              if (!$ret) $texte.=$tabChaine[$i]->text;  //an error occurs (unknown charset)                           else $texte.=$ret;                        }                                    break;            }        }            return $texte;        }?>
To Top