PHP decode GB2312

2019-09-06 07:50发布

I'm working on an IMAP email script and I have some lines coded in GB2312 (which I assume is Chinese encoding), looks like this =?GB2312?B?foobarbazetc

How can I start working with this string? I checked mb_list_encodings() and this one is not listed.

2条回答
迷人小祖宗
2楼-- · 2019-09-06 08:37

If you have the base64-decoded data, then use mbstring or iconv. If you have the raw header, then mbstring.

<?php
$t = "\xc4\xe3\xba\xc3\n";
echo iconv('GB2312', 'UTF-8', $t);
echo mb_convert_encoding($t, 'UTF-8', 'GB2312');

mb_internal_encoding('UTF-8');
echo mb_decode_mimeheader("=?gb2312?b?xOO6ww==?=");
?>
查看更多
迷人小祖宗
3楼-- · 2019-09-06 08:46

Ignacio solved the meat of the problem with mb_decode_mimeheader() but for future reference these links are also helpful:

The specific header string I was working with:

$subject = "=?GB2312?B?tPC4tDogUXVvdGF0aW9uIFBJSSBwcm9kdWN0cyA=?= =?GB2312?B?Rk9CIFNoYW5naGFpIG9yIE5pbmdibyBwb3J0?="

This required a page header of

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

and PHP

mb_internal_encoding('utf-8');
echo mb_decode_mimeheader($subject)."<br />";

to output

主题: Quotation PII products FOB Shanghai or Ningbo port
查看更多
登录 后发表回答