如何解码Unicode转义序列,如“\\\”适当的UTF-8编码字符? [英] How to decode Unicode escape sequences like "\u00ed" to proper UTF-8 encoded characters?

查看:226
本文介绍了如何解码Unicode转义序列,如“\\\”适当的UTF-8编码字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中有一个函数可以解码Unicode转义序列,如 \\\í í 和所有其他类似的事件?



我发现类似的问题这里,但似乎不起作用。

解决方案

尝试这样:

  $ str = preg_replace_callback('/ \\\\ 0-9a-fA-F] {4})/',function($ match){
return mb_convert_encoding(pack('H *',$ match [1]),'UTF-8' -2BE');
},$ str);

如果是基于UTF-16的C / C ++ / Java / Json样式:

  $ str = preg_replace_callback('/ \\\\u([0-9a-fA-F] {4}) /',function($ match){
return mb_convert_encoding(pack('H *',$ match [1]),'UTF-8','UTF-16BE');
},$ STR);


Is there a function in PHP that can decode Unicode escape sequences like "\u00ed" to "í" and all other similar occurrences?

I found similar question here but is doesn't seem to work.

解决方案

Try this:

$str = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
    return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}, $str);

In case it's UTF-16 based C/C++/Java/Json-style:

$str = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
    return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UTF-16BE');
}, $str);

这篇关于如何解码Unicode转义序列,如“\\\”适当的UTF-8编码字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆