PHP 字符串中的 Unicode 字符

2021-12-26 00:00:00 unicode php

这个问题看起来很简单,但我一直没能找到答案.

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("x10x00", 'UTF-8', 'UTF-16BE');

相关文章