Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : IMAP, POP3 and NNTP Functions : imap_utf8

imap_utf8

Converts MIME-encoded text to UTF-8 (PHP 4, PHP 5)
string imap_utf8 ( string mime_encoded_text )


Code Examples / Notes » imap_utf8

anonymous webmaster@luzon14dotcom

You may also try this example:
$_subject = "=?iso-8859-1?b?THV6b26uMTQ=?= dot CoM";
echo utf8_decode(imap_utf8($_subject));
OUTPUT: Luzon®14 dot CoM
Regards,
Tuso/Nazareth


mogster_at_boomdesign.no

To get a complete decode to utf8 (which is the protocol with "national" characters), use these two:
$subject = "=?ISO-8859-1?Q?B=F8lla?=, med =?ISO-8859-1?Q?=F8l?= i baggen";
$subject = imap_utf8($subject);
$subject = utf8_decode($subject);
will return the subject in 8-bit ascii.
That is, both functions.
knutm :-)


aperez

I got issues decoding strings with spanish characters. I found encodeISO88591() function at this manual as an user note.
After having problems with imap_utf8() and utf8_decode(), I've decided create a function decodeISO88591() for decoding strings encoded with encodeISO88591().
Here's the code(I've translated remarks and variable and array names from spanish to english for better understanding):
function encodeISO88591($string)
{
 // ISO-8859-1 string header
 $stringISO = "=?iso-8859-1?q?";
 
 // Each character are encoded('equal-to' symbol + hexadecimal value from ASCII code)
 for($i=0;$i<strlen($string);$i++)
 {
   // Basic ASCII characters are not encoded
   if(ord(substr($string,$i,1))<1 || ord(substr($string,$i,1))>127)
   {
     $char = ord($string[$i]);
     $char = strtoupper(dechex($char));
     $stringISO.="=".$char;
   }
   else
   {
     $stringISO.=substr($string,$i,1);
   }
 }
 
 // ISO-8859-1 string footer
 $stringISO.="?= ";
 
 return($stringISO);
}
// And this is my function decodeISO88591()
function decodeISO88591($string)
{
 // Arrays for obtaining hexadecimal values
 // for each ISO-8859-1 charset
 $mAlfa=array("A","B","C","D","E","F");
 $mNum=array();
 for($n=0;$n<10;$n++)
 {
   $mNum[]=$n;
 }
 
 // ISO-8859-1 charset
 $iso88591=array(" ","¡","¢","£","¤",
                        "¥","¦","§","¨","©","ª",
                        "«","¬","­","®","¯","°",
                        "±","²","³","´","µ","¶",
                        "·","¸","¹","º","»","¼",
                        "½","¾","¿","À","Á","Â",
                        "Ã","Ä","Å","Æ","Ç","È",
                        "É","Ê","Ë","Ì","Í","Î","Ï",
                        "Ð","Ñ","Ò","Ó","Ô","Õ",
                        "Ö","×","Ø","Ù","Ú","Û",
                        "Ü","Ý","Þ","ß","à","á","â",
                        "ã","ä","å","æ","ç","è","é",
                        "ê","ë","ì","í","î","ï","ð","ñ",
                        "ò","ó","ô","õ","ö","÷","ø",
                        "ù","ú","û","ü","ý","þ","ÿ");
 // Hexadecimal values array
 for($a=0;$a<sizeof($mAlfa);$a++)
 {
   for($n=0;$n<sizeof($mNum);$n++)
   {
     $mHex[]=$mAlfa[$a].$mNum[$n];
   }
   for($a2=0;$a2<sizeof($mAlfa);$a2++)
   {
     $mHex[]=$mAlfa[$a].$mAlfa[$a2];
   }
 }
 
 // ISO-8859-1 string header and footer are deleted
 $string=str_replace("=?iso-8859-1?q?","",$string);
 $string=str_replace("?= ","",$string);
 
 // Encoded values are decoded
 for($h=0;$h<sizeof($mHex);$h++)
 {
   $string=str_replace(("=".$mHex[$h]),$iso88591[$h],$string);
 }
 
 return($string);
}
I hope this helps somebody. :)


hans

Also see:
<?php
imap_mime_header_decode($sting);
?>
Don't try to decode subjects with this one... imap_mime_header_decode does a much better job at that.


marcelo

# this script are correct with the right signs remove expression
# please, ignore the later.
function decode_utf8($str){
      # paterns
          $err="(=\?.{10,13}q\?_?|\?\=)";
          $pat = "/=([0-9A-F]{2})/";
          $cha="'.chr(hexdec(";
      # erase null signs in string
         $str=eregi_replace($err,"",$str);
      # to decode with eval and replace
         eval("\$str='".
                 preg_replace($pat,$cha."'$1')).'",$str)
                 ."';");
       # return
          return $str;
}
#Note: you can to reduce this script in 3 lines.


Change Language


Follow Navioo On Twitter
imap_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
eXTReMe Tracker