您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP解码unicode编码

2026/1/29 9:30:30发布17次查看
这篇文章主要介绍了php解码unicode编码  ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
function unicode_decode($name) { //方法一 $name = str_replace("\\\\u","\u",$name); $json = '{"str":"'.$name.'"}'; $arr = json_decode($json,true); if(empty($arr)) return ''; return $arr['str']; //方法二 // 转换编码,将unicode编码转换成可以浏览的utf-8编码 $pattern = '/([\w]+)|(\\\u([\w]{4}))/i'; preg_match_all($pattern, $name, $matches); if (!empty($matches)) { $name = ''; for ($j = 0; $j < count($matches[0]); $j++) { $str = $matches[0][$j]; if (strpos($str, '\\u') === 0) { $code = base_convert(substr($str, 2, 2), 16, 10); $code2 = base_convert(substr($str, 4), 16, 10); $c = chr($code).chr($code2); $c = iconv('ucs-2', 'utf-8', $c); $name .= $c; } else { $name .= $str; } } } return $name; }
以下是网上找的方法:
方案a(稳定版+推荐):
function replace_unicode_escape_sequence($match) { return mb_convert_encoding(pack('h*', $match[1]), 'utf-8', 'ucs-2be'); }$name = '\u65b0\u6d6a\u5fae\u535a';$str = preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $name);echo $str; //输出: 新浪微博
//咱将上述方案a给封装起来~~~(方案a稳定版+升级+推荐)class helper_tool{ static function unicodedecode($data) { function replace_unicode_escape_sequence($match) { return mb_convert_encoding(pack('h*', $match[1]), 'utf-8', 'ucs-2be'); } $rs = preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $data); return $rs; } }//调用$name = '\u65b0\u6d6a\u5fae\u535a';$data = helper_tool::unicodedecode($name); //输出新浪微博
方案b(次推荐):
<?phpfunction unicodedecode($name){ $json = '{"str":"'.$name.'"}'; $arr = json_decode($json,true); if(empty($arr)) return ''; return $arr['str']; }$name = '\u65b0\u6d6a\u5fae\u535a';echo unicodedecode($name); //输出: 新浪微博
相关推荐:
php解码js使用escape转码的函数
以上就是php解码unicode编码 的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product