PHP字符串中的Unicode字符 [英] Unicode character in PHP string
问题描述
这个问题看起来非常简单,但我一直找不到答案.
This question looks embarrassingly simple, but I haven't been able to find an answer.
与下面的C#代码行等效的PHP是什么?
What is the PHP equivalent to the following C# line of code?
string str = "\u1000";
此示例创建一个带有单个Unicode字符的字符串,其"Unicode数值"为十六进制的1000(十进制的4096).
This sample creates a string with a single Unicode character whose "Unicode numeric value" is 1000 in hexadecimal (4096 in decimal).
也就是说,在PHP中,如何创建一个具有"Unicode数值"已知的Unicode字符的字符串?
That is, in PHP, how can I create a string with a single Unicode character whose "Unicode numeric value" is known?
推荐答案
因为JSON直接支持\uxxxx
语法,所以我想到的第一件事是:
Because JSON directly supports the \uxxxx
syntax the first thing that comes into my mind is:
$unicodeChar = '\u1000';
echo json_decode('"'.$unicodeChar.'"');
另一种选择是使用mb_convert_encoding()
echo mb_convert_encoding('က', 'UTF-8', 'HTML-ENTITIES');
或利用UTF-16BE(大端)和Unicode代码点之间的直接映射:
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');
这篇关于PHP字符串中的Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!