【问题标题】:Creating a UTF-8 string from hexadecimal code从十六进制代码创建 UTF-8 字符串
【发布时间】:2013-04-12 12:01:44
【问题描述】:

在 C++ 中,可以使用这种表示法创建 UTF-8 字符串:"\uD840\uDC50"

但是这在 PHP 中不起作用。有没有类似的符号?

如果没有,是否有任何内置方法可以创建一个知道其 Unicode 代码点的 UTF-8 字符串?

【问题讨论】:

标签: php unicode utf-8 hex codepoint


【解决方案1】:
function hexToString($str){return chr(hexdec(substr($str, 2)));}
$result = preg_replace_callback("/(\\\\x..)/isU", function($m) { return hexToString($m[0] ); }, $str);

【讨论】:

    【解决方案2】:

    我最终是这样实现的:

    $utf8 = html_entity_decode("一", ENT_COMPAT, 'UTF-8');
    

    【讨论】:

    • 使用 ENT_QUOTES | ENT_COMPAT 也可以转换引号
    • 这有限制,不适用于所有 UTF-8 字符,因为并非所有十六进制字符都支持 HTML 标准。参见ascii.cl/htmlcodes.htm(“未在 HTML 4 标准中定义”)
    猜你喜欢
    • 2014-08-07
    • 2015-12-06
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    相关资源
    最近更新 更多