Search code examples
phpunicodeencodingutf-8utf-16

PHP - UTF-16 to UTF-8(hex) conversion


Is it possible to convert UTF-16
U+610F
style character to UTF-8 (hex)
E6848F
using PHP ?

UTF-8 character is '意'


Solution

  • From the comments in the chr man page, one quick hack for turning an ordinal character number into a UTF-8 byte sequence:

    function unichr($u) {
        return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8', 'HTML-ENTITIES');
    }
    
    // unichr(0x610F) -> "\xE6\x84\x8F"