Unicode character in PHP string

Telaclavo picture Telaclavo · May 19, 2011 · Viewed 202.7k times · Source

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?

string str = "\u1000";

This sample creates a string with a single Unicode character whose "Unicode numeric value" is 1000 in hexadecimal (4096 in decimal).

That is, in PHP, how can I create a string with a single Unicode character whose "Unicode numeric value" is known?

Answer

Stefan Gehrig picture Stefan Gehrig · May 19, 2011

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');