开发者

Output UTF-16? A little stuck

I have some UTF-16 encoded characters in their surrogate pair form. I want to output those surrogate pairs as characters on the screen.

Does anyone know how t开发者_JS百科his is possible?


iconv('UTF-16', 'UTF-8', yourString)


Your question is a little unclear.

If you have ASCII text with embedded UTF-16 escape sequences, you can convert everything to UTF-8 in this way:

function unescape_utf16($string) {
    /* go for possible surrogate pairs first */
    $string = preg_replace_callback(
        '/\\\\u(D[89ab][0-9a-f]{2})\\\\u(D[c-f][0-9a-f]{2})/i',
        function ($matches) {
            $d = pack("H*", $matches[1].$matches[2]);
            return mb_convert_encoding($d, "UTF-8", "UTF-16BE");
        }, $string);
    /* now the rest */
    $string = preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
        function ($matches) {
            $d = pack("H*", $matches[1]);
            return mb_convert_encoding($d, "UTF-8", "UTF-16BE");
        }, $string);
    return $string;
}

$string = '\uD869\uDED6';
echo unescape_utf16($string);

which gives the character

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜