note 71762 added to function.imap-mime-header-decode
| From: | diegonunes | Date: | Thu, 14 Dec 2006 18:12:59 +0000 |
| Subject: | note 71762 added to function.imap-mime-header-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120612@lists.php.net to get a copy of this message | ||
The previous comment (from hans) seems to make no sense at all, since it will not change the
encoding and possibly result in a "multiencoding" string (that the browser and anything
else will be unable to render, of course).
I use a little function to decode the whole header to a specified encoding. It is as follow:
<?php
//return supported encodings in lowercase.
function mb_list_lowerencodings() { $r=mb_list_encodings();
for ($n=sizeOf($r); $n--; ) { $r[$n]=strtolower($r[$n]); } return $r;
}
// Receive a string with a mail header and returns it
// decoded to a specified charset.
// If the charset specified into a piece of text from header
// isn't supported by "mb", the "fallbackCharset" will be
// used to try to decode it.
function decodeMimeString($mimeStr, $inputCharset='utf-8',
$targetCharset='utf-8', $fallbackCharset='iso-8859-1') {
$encodings=mb_list_lowerencodings();
$inputCharset=strtolower($inputCharset);
$targetCharset=strtolower($targetCharset);
$fallbackCharset=strtolower($fallbackCharset);
$decodedStr='';
$mimeStrs=imap_mime_header_decode($mimeStr);
for ($n=sizeOf($mimeStrs), $i=0; $i<$n; $i++) {
$mimeStr=$mimeStrs[$i];
$mimeStr->charset=strtolower($mimeStr->charset);
if (($mimeStr == 'default' && $inputCharset == $targetCharset)
|| $mimStr->charset == $targetCharset) {
$decodedStr.=$mimStr->text;
} else {
$decodedStr.=mb_convert_encoding(
$mimeStr->text, $targetCharset,
(in_array($mimeStr->charset, $encodings) ?
$mimeStr->charset : $fallbackCharset)
)
);
}
} return $decodedStr;
}
?>
Hope it helps.
----
Server IP: 200.185.109.13
Probable Submitter: 200.185.109.125
----
Manual Page -- http://www.php.net/manual/en/function.imap-mime-header-decode.php
Edit -- https://master.php.net/note/edit/71762
Del: integrated -- https://master.php.net/note/delete/71762/integrated
Del: useless -- https://master.php.net/note/delete/71762/useless
Del: bad code -- https://master.php.net/note/delete/71762/bad+code
Del: spam -- https://master.php.net/note/delete/71762/spam
Del: non-english -- https://master.php.net/note/delete/71762/non-english
Del: in docs -- https://master.php.net/note/delete/71762/in+docs
Del: other reasons-- https://master.php.net/note/delete/71762
Reject -- https://master.php.net/note/reject/71762
Search -- https://master.php.net/manage/user-notes.php