A nice way to decode strings is to use the mb_list_encodings(), but I had two problems with it:
sometimes, the charset is uppercase in the header an lowercase in mb_list_encodings() and sometimes, the charset is not in the mb_list_encodings() list.
<?php
function upperListEncode() { $encodes=mb_list_encodings();
foreach ($encodes as $encode) $tencode[]=strtoupper($encode);
return $tencode;
}
function decode($string) {
$tabChaine=imap_mime_header_decode($string);
$texte='';
for ($i=0; $i<count($tabChaine); $i++) {
switch (strtoupper($tabChaine[$i]->charset)) { case 'UTF-8': $texte.= $tabChaine[$i]->text; break;
case 'DEFAULT': $texte.= $tabChaine[$i]->text; break;
default: if (in_array(strtoupper($tabChaine[$i]->charset),upperListEncode())) {$texte.= mb_convert_encoding($tabChaine[$i]->text,'UTF-8',$tabChaine[$i]->charset);}
else { $ret = iconv($tabChaine[$i]->charset, "UTF-8", $tabChaine[$i]->text);
if (!$ret) $texte.=$tabChaine[$i]->text; else $texte.=$ret;
}
break;
}
}
return $texte;
}
?>