【问题标题】:php chr with unicode values带有unicode值的php chr
【发布时间】:2011-02-20 20:11:52
【问题描述】:

我可以在 php 中使用字符的 unicode 值(例如 U+0021 代表 !)吗?并将其转换为打印中的原始字符(使用chr()或其他功能)?

【问题讨论】:

标签: php unicode


【解决方案1】:
function replace_unicode_escape_sequence($match) {
    return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}
function unicode_chr ($chr) {
    $x = explode("+", $chr);
    $str = "\u".end($x);
    return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $str);
}

var_dump(unicode_chr("U+0021")); // string(1) "!"

改编自:How to decode Unicode escape sequences like "\u00ed" to proper UTF-8 encoded characters?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多