note 101150 deleted from function.imap-mime-header-decode by danbrown
| From: | danbrown@php.net | Date: | Mon, 29 Nov 2010 23:10:54 +0000 |
| Subject: | note 101150 deleted from function.imap-mime-header-decode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174457@lists.php.net to get a copy of this message | ||
Note Submitter: anonymouse at example dot com
----
both functions called mime_decode_string() below are broken. i made some syntax changes for
readability and style:
// return a string with a mail header
// decoded to a specified charset. if the charset
// isn't supported by mb_convert_encoding(),
// def_charset will be used to decode it.
function decode_mime_string($mime_str, $in_charset='utf-8',
$target_charset='utf-8', $def_charset='iso-8859-1')
{
// valid encodings in lowercase array
$encs = array_flip(array_change_key_case(array_flip(mb_list_encodings())));
$in_charset = strtolower($in_charset);
$target_charset = strtolower($target_charset);
$def_charset = strtolower($def_charset);
$decoded_str = '';
$mime_strs = imap_mime_header_decode($mime_str);
$charset_match = ($in_charset === $target_charset);
foreach ($mime_strs as $mime_str)
{
$mime_str->charset = strtolower($mime_str->charset);
if ( ( $mime_str === 'default' AND $charset_match ) OR
( $mime_str->charset === $target_charset ) )
{
$decoded_str .= $mime_str->text;
}
else
{
$charset = ( in_array( $mime_str->charset, $encs ) ) ? $mime_str->charset : $def_charset;
$decoded_str .= mb_convert_encoding( $mime_str->text, $target_charset, $charset );
}
}
return $decoded_str;
}