美文网首页
一段Unicode转中文的代码

一段Unicode转中文的代码

作者: 苏大发 | 来源:发表于2020-02-28 23:14 被阅读0次
    <?php
    $a = '["u8d27u68af","u505cu8f66u4f4d","u6696u6c14","u6276u68af","u4e0au6c34","u4e0bu6c34","u6392u70df","u6392u6c61","u7ba1u7164","380V","u53efu660eu706b","u5916u6446u533a"]';
    
    $b = json_decode($a);
    $array = [];
    foreach($b as $k=>$v){
        $arr = explode('u',$v);
        $arr = array_slice($arr,1,count($arr));
        foreach($arr as $k=>$v){
            $arr[$k] = "\u".$v;
        }
        $str = implode('', $arr);
        $array[] = $str;
    }
        //Unicode转汉字
    foreach($array as $k=>$v){
        $a = decodeUnicode($v);
        $array[$k] = $a;
    }
    var_dump($array);
    
    function decodeUnicode($str)
    
    {
    
        return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
            function ($matches) {
                return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");
            },
    
            $str);
    
    }
    
    image.png

    相关文章

      网友评论

          本文标题:一段Unicode转中文的代码

          本文链接:https://www.haomeiwen.com/subject/lmdbwqtx.html