Unicode character in PHP string

前端 未结 8 1660
生来不讨喜
生来不讨喜 2020-11-22 14:17

This question looks embarrassingly simple, but I haven\'t been able to find an answer.

What is the PHP equivalent to the following C# line of code?

s         


        
相关标签:
8条回答
  • 2020-11-22 14:53

    Because JSON directly supports the \uxxxx syntax the first thing that comes into my mind is:

    $unicodeChar = '\u1000';
    echo json_decode('"'.$unicodeChar.'"');
    

    Another option would be to use mb_convert_encoding()

    echo mb_convert_encoding('က', 'UTF-8', 'HTML-ENTITIES');
    

    or make use of the direct mapping between UTF-16BE (big endian) and the Unicode codepoint:

    echo mb_convert_encoding("\x10\x00", 'UTF-8', 'UTF-16BE');
    
    0 讨论(0)
  • 2020-11-22 14:53

    Try Portable UTF-8:

    $str = utf8_chr( 0x1000 );
    $str = utf8_chr( '\u1000' );
    $str = utf8_chr( 4096 );
    

    All work exactly the same way. You can get the codepoint of a character with utf8_ord(). Read more about Portable UTF-8.

    0 讨论(0)
提交回复
热议问题