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. :)