|
imap_mime_header_decode
Decode MIME header elements
(PHP 4, PHP 5)
Decodes MIME message header extensions that are non ASCII text (see » RFC2047). Return Values
The decoded elements are returned in an array of objects, where each
object has two properties,
If the element hasn't been encoded, and in other words is in
plain US-ASCII, the ExamplesExample 1056. imap_mime_header_decode() example<?php The above example will output: Charset: ISO-8859-1 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.
See Also
Code Examples / Notes » imap_mime_header_decodehans
This is obvious, but nevertheless here is a "flat" version: <?php private function flatMimeDecode($string) { $array = imap_mime_header_decode($string); $str = ""; foreach ($array as $key => $part) { $str .= $part->text; } return $str; } ?> mattias
This function does not code a-z 0-9 like the function posted by bandpay at hotmail dot com function encode_iso88591($string) { if( ereg("[^A-Za-z0-9\ ]", $string) ) { $text = "=?iso-8859-1?q?"; for( $i = 0 ; $i < strlen($string) ; $i++ ) { if( ereg("[^A-Za-z0-9]", $string[$i]) ) { $text .= "=".dechex(ord($string[$i])); } else { $text .= $string[$i]; } } return $text."?="; } else return $string; } diego nunes
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. cracker
In response to Sven dot Dickert at planb dot de: if you encounter problems with "=?utf-8?Q?" appearing in your headers, I found that simply using "imap_utf8($string)" decoded the "$string" properly and solved my problem perfectly.
hdlim
imap_mime_header_decode, utf-7 and utf-8 problem, i solved the problem using below function. note that iconv function for code converting. you must replace "EUC-KR" as iconv parameter with charset you want such as "iso-8859-1". function mime_decode($s) { $elements = imap_mime_header_decode($s); for($i = 0;$i < count($elements);$i++) { $charset = $elements[$i]->charset; $text =$elements[$i]->text; if(!strcasecmp($charset, "utf-8") || !strcasecmp($charset, "utf-7")) { $text = iconv($charset, "EUC-KR", $text); } $decoded = $decoded . $text; } return $decoded; } charsyam
I wrote a simple ks_c_5601-1987(2byte) encode function encode_ksc5601( $string ) { $encode = base64_encode( $string ); $text = "=?ks_c_5601-1987?B?"; $text = $text.$encode."?="; return $text; } sven dot dickert
Beware! imap_mime_header_decode in 4.0.6 is _not_ RFC2047 conform. The string =?utf-7?Q?Petra_M+APw-ller?= will not converted into Petra Müller cause the charset utf7 is unknown. Same goes to =?utf-8?Q?Petra_M=C3=BCller?= for charset utf8.
pawel tomicki
<?php function decode_iso88591($string){ if (strpos(strtolower($string), '=?iso-8859-1') === false) { return $string; } $string = explode('?', $string); return strtolower($string[2]) == 'q' ? quoted_printable_decode($string[3]) : base64_decode($string[3]); } ?> |
Change Languageimap_8bit imap_alerts imap_append imap_base64 imap_binary imap_body imap_bodystruct imap_check imap_clearflag_full imap_close imap_createmailbox imap_delete imap_deletemailbox imap_errors imap_expunge imap_fetch_overview imap_fetchbody imap_fetchheader imap_fetchstructure imap_get_quota imap_get_quotaroot imap_getacl imap_getmailboxes imap_getsubscribed imap_header imap_headerinfo imap_headers imap_last_error imap_list imap_listmailbox imap_listscan imap_listsubscribed imap_lsub imap_mail_compose imap_mail_copy imap_mail_move imap_mail imap_mailboxmsginfo imap_mime_header_decode imap_msgno imap_num_msg imap_num_recent imap_open imap_ping imap_qprint imap_renamemailbox imap_reopen imap_rfc822_parse_adrlist imap_rfc822_parse_headers imap_rfc822_write_address imap_savebody imap_scanmailbox imap_search imap_set_quota imap_setacl imap_setflag_full imap_sort imap_status imap_subscribe imap_thread imap_timeout imap_uid imap_undelete imap_unsubscribe imap_utf7_decode imap_utf7_encode imap_utf8 |