2

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 this is possible?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Jamie Redmond
  • 63
  • 2
  • 6
  • 1
    http://stackoverflow.com/questions/3506120/unicode-surrogate-pairs-question Seems like a dup. – Jim Aug 17 '10 at 21:13

2 Answers2

3

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

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
1

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 in UTF-8 (requires 4 bytes since it's outside the BMP).

If all your text is UTF-16 (including HTML tags, etc.), you could simply tell the browser the output is in UTF-16:

header("Content-type: text/html; charset=UTF-16");

This is very rare, because PHP scripts cannot be written in UTF-16 (unless PHP is compiled with multibyte support), which would make printing literal strings awkward.

So you probably only have a piece of text in UTF-16 that you want to convert to whatever encoding your webpage is using. You can do this conversion with:

//replace UTF-8 with your actual page encoding
mb_convert_encoding($string, "UTF-8", "UTF-16");
Artefacto
  • 96,375
  • 17
  • 202
  • 225