note 111564 added to function.imap-mime-header-decode

From: Date: Mon, 04 Mar 2013 21:42:05 +0000
Subject: note 111564 added to function.imap-mime-header-decode
Groups: php.notes 
Request: Send a blank email to php-notes+get-193894@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 69.147.83.199 Probable Submitter: 90.32.97.221 ---- Manual Page -- http://php.net/manual/en/function.imap-mime-header-decode.php Edit -- https://master.php.net/note/edit/111564 Del: integrated -- https://master.php.net/note/delete/111564/integrated Del: useless -- https://master.php.net/note/delete/111564/useless Del: bad code -- https://master.php.net/note/delete/111564/bad+code Del: spam -- https://master.php.net/note/delete/111564/spam Del: non-english -- https://master.php.net/note/delete/111564/non-english Del: in docs -- https://master.php.net/note/delete/111564/in+docs Del: other reasons-- https://master.php.net/note/delete/111564 Reject -- https://master.php.net/note/reject/111564 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#193894) next »