PHP 8.4.1 Released!

imap_mime_header_decode

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

imap_mime_header_decodeDecode MIME header elements

Descrizione

imap_mime_header_decode(string $string): array|false

Decodes MIME message header extensions that are non ASCII text (see » RFC2047).

Elenco dei parametri

string

The MIME text

Valori restituiti

The decoded elements are returned in an array of objects, where each object has two properties, charset and text.

If the element hasn't been encoded, and in other words is in plain US-ASCII, the charset property of that element is set to default.

The function returns false on failure.

Esempi

Example #1 imap_mime_header_decode() example

<?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
"Text: {$elements[$i]->text}\n\n";
}
?>

Il precedente esempio visualizzerà:

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

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

In the above example we would have two elements, whereas the first element had previously been encoded with ISO-8859-1, and the second element would be plain US-ASCII.

Vedere anche:

add a note

User Contributed Notes 1 note

up
9
Anonymous
11 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.

<?php
function 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