note 43136 added to function.imap-utf8

From: Date: Thu, 10 Jun 2004 16:05:01 +0000
Subject: note 43136 added to function.imap-utf8
Groups: php.notes 
Request: Send a blank email to php-notes+get-70887@lists.php.net to get a copy of this message
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. :) ---- Manual Page -- http://www.php.net/manual/en/function.imap-utf8.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+43136 Delete -- http://master.php.net/manage/user-notes.php?action=delete+43136&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+43136&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#70887) next »