Search code examples
phpunicodesurrogate-pairs

Unicode surrogate pairs


Say I have a surrogate pair. For example:

\u306f\u30fc

Is there a function I can use to print the character to the screen?


Solution

  • If you want to do it manually:

    echo chr(0x30) . chr(0x6f) . chr(0x30) . chr(0xfc);
    

    If you have the string, you could always do:

    $callback = function($match) { 
        return chr(hexdec($match[1])) . chr(hexdec($match[2]));
    }
    preg_replace_callback('#\\\\u([0-9a-f]{2})([0-9a-f]{2})#', $callback, $string);
    

    Or, if php < 5.3

    $callback = create_function('$match', 
        'return chr(hexdec($match[1])) . chr(hexdec($match[2]));'
    );
    preg_replace_callback('#\\\\u([0-9a-f]{2})([0-9a-f]{2})#', $callback, $string);