简体   繁体   English

php chr与unicode值

[英]php chr with unicode values

Can I use unicode value of a character (for example U+0021 for ! ) in php? 我可以在php中使用字符的unicode值(例如U+0021 ! )吗? and convert it to original character in printing (with chr() or other functions)? 并在打印时将其转换为原始字符(使用chr()或其他功能)?

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) "!"

Adapted from: How to decode Unicode escape sequences like "\í" to proper UTF-8 encoded characters? 改编自: 如何将“\\ u00ed”等Unicode转义序列解码为正确的UTF-8编码字符?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM