PHP - 将 unicode 转换为字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7274183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:22:38  来源:igfitidea点击:

PHP - convert unicode to character

phpunicode

提问by jazz

Possible Duplicate:
How to get the character from unicode value in PHP?
PHP: Convert unicode codepoint to UTF-8

可能的重复:
如何从 PHP 中的 unicode 值中获取字符?
PHP:将 unicode 代码点转换为 UTF-8

How can I convert a unicode character such as %u05E1to a normal character via PHP?

如何%u05E1通过 PHP将 unicode 字符(例如)转换为普通字符?

The chrfunction not covering it and I am looking for something similar.

chr功能不会覆盖它,我期待类似的东西。

回答by deceze

"%uXXXX" is a non-standard scheme for URL-encoding Unicode characters. Apparently it was proposed but never really used. As such, there's hardly any standard function that can decode it into an actual UTF-8 sequence.

"%uXXXX" 是用于 URL 编码 Unicode 字符的非标准方案。显然它被提议但从未真正使用过。因此,几乎没有任何标准函数可以将其解码为实际的 UTF-8 序列。

It's not too difficult to do it yourself though:

不过,自己做并不难:

$string = '%u05E1%u05E2';
$string = preg_replace('/%u([0-9A-F]+)/', '&#x;', $string);
echo html_entity_decode($string, ENT_COMPAT, 'UTF-8');

This converts the %uXXXXnotation to HTML entity notation &#xXXXX;, which can be decoded to actual UTF-8 by html_entity_decode. The above outputs the characters "??" in UTF-8 encoding.

这会将%uXXXX表示法转换为 HTML 实体表示法&#xXXXX;,它可以通过html_entity_decode. 以上输出字符“??” UTF-8 编码。

回答by Alex Turpin

Use hexdecto convert it to it's decimal representation first.

用于hexdec首先将其转换为十进制表示。

echo chr(hexdec("05E1"));
var_dump(hexdec("%u05E1") == hexdec("05E1")); //true