Search code examples
phpcharacter-encodingdecodegb2312

PHP decode GB2312


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.


Solution

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