Unicode character in PHP string

前端 未结 8 1673
生来不讨喜
生来不讨喜 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:29

    I wonder why no one has mentioned this yet, but you can do an almost equivalent version using escape sequences in double quoted strings:

    \x[0-9A-Fa-f]{1,2}

    The sequence of characters matching the regular expression is a character in hexadecimal notation.

    ASCII example:

    
    

    Hello World!

    So for your case, all you need to do is $str = "\x30\xA2";. But these are bytes, not characters. The byte representation of the Unicode codepoint coincides with UTF-16 big endian, so we could print it out directly as such:

    
    

    If you are using a different encoding, you'll need alter the bytes accordingly (mostly done with a library, though possible by hand too).

    UTF-16 little endian example:

    
    

    UTF-8 example:

    
    

    There is also the pack function, but you can expect it to be slow.

提交回复
热议问题