PHP 中是否有一个函数可以解码 Unicode 转义序列,例如“\u00ed
”到“í
”以及所有其他类似的出现?
我发现类似的问题here但似乎不起作用。
最佳答案
试试这个:
$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);
如果它是基于 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);
关于php - 如何将 Unicode 转义序列(如 "\u00ed")解码为正确的 UTF-8 编码字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21439846/