imap_mime_header_decode

(PHP 3 >= 3.0.17, PHP 4, PHP 5)

imap_mime_header_decode -- Decode MIME header elements

Description

array imap_mime_header_decode ( string text )

imap_mime_header_decode() function decodes MIME message header extensions that are non ASCII text (see RFC2047) 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".

例子 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";
}
?>

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.


add a note add a note User Contributed Notes
hans at lintoo dot dk
22-Dec-2005 10:10
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;
}
?>
cracker at bigcracker dot ca
22-Feb-2005 08:55
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.
Pawel Tomicki <pafka at klub dot chip dot pl>
23-Apr-2004 02:04
<?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]);
}

?>
mattias at forss dot se
27-Nov-2003 11:45
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;
}
hdlim at kis21 dot com
02-Jan-2003 02:35
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;
}
Sven dot Dickert at planb dot de
16-May-2002 09:31
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 Mller cause the charset utf7 is unknown.  Same goes to =?utf-8?Q?Petra_M=C3=BCller?= for charset utf8.
charsyam at liso dot cs dot pusan dot ac dot kr
13-Aug-2001 11:34
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;
}