在一个 php 网站上,我从 imap 收到电子邮件并将它们保存在数据库中。
另一方面,我想展示其中的一些。那个邮箱接收很多英文邮件,也有日文和中文。
以下代码的问题是我无法检测到所有字符集。如果我安排数组的顺序,这样中文字符就可以了,那么其他字符集就错了。
<?php
$subject = "板イテ淌"; // can be japanese
$subject = "这间面积70平"; // can be chinese
$subject = "This string can have latin1 chars also";
function get_subject($subject);
$encs = array();
$enc[] = "Big5";
$enc[] = "big5";
$enc[] = "euc-kr";
$enc[] = "EU-CN";
$enc[] = "GB2312";
$enc[] = "ISO-8859-1";
$enc[] = "GBK";
$enc[] = "CP936";
$enc[] = "ASCII";
$enc[] = "JIS";
$enc[] = "UTF-8";
$enc[] = "EUC-JP";
$enc[] = "SJIS";
$enc[] = "latin1";
$encoding = mb_detect_encoding($this->object_message, $encs);
$subject = mb_convert_encoding($this->object_message, 'UTF-8', $encoding);
$subject = iconv ( 'utf-8', 'ISO-8859-2' , $subject );
return $subject;
?>