Voting

: max(five, two)?
(Example: nine)

The Note You're Voting On

Anonymous
12 years ago
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() { //convert mb_list_encodings() to uppercase
$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)) { //convert charset to uppercase
case 'UTF-8': $texte.= $tabChaine[$i]->text; //utf8 is ok
break;
case
'DEFAULT': $texte.= $tabChaine[$i]->text; //no convert
break;
default: if (
in_array(strtoupper($tabChaine[$i]->charset),upperListEncode())) //found in mb_list_encodings()
{$texte.= mb_convert_encoding($tabChaine[$i]->text,'UTF-8',$tabChaine[$i]->charset);}
else {
//try to convert with iconv()
$ret = iconv($tabChaine[$i]->charset, "UTF-8", $tabChaine[$i]->text);
if (!
$ret) $texte.=$tabChaine[$i]->text; //an error occurs (unknown charset)
else $texte.=$ret;
}
break;
}
}

return
$texte;
}
?>

<< Back to user notes page

To Top