PHP decode GB2312

2019-09-06 08:38发布

问题:

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.

回答1:

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==?=");
?>


回答2:

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

  • http://developer.loftdigital.com/blog/php-utf-8-cheatsheet
  • http://www.herongyang.com/PHP-Chinese/PHP-UTF-8-Chinese-String-Literals.html

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