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

php 编码转换_PHP教程

2025/8/2 16:24:34发布22次查看
编码转换这段代码是thinkphp框架中的,感觉很常用,所以单独拎出来,大家共同学习一下。
function auto_charset($fcontents,$from='',$to='')
{
    if( strtoupper($from) === strtoupper($to) || empty($fcontents) || (is_scalar($fcontents) && !is_string($fcontents)) ){
        //如果编码相同或者非字符串标量则不转换
        return $fcontents;
    }
    $from   =  strtoupper($from)=='utf8'? 'utf-8':$from;
    $to       =  strtoupper($to)=='utf8'? 'utf-8':$to;
    if(is_string($fcontents) ) {
                if(function_exists('mb_convert_encoding')){
            return mb_convert_encoding ($fcontents, $to, $from);
        }elseif(function_exists('iconv')){
            return iconv($from,$to,$fcontents);
        }else{
            exit('转换失败');
            return $fcontents;
        }
    }
    elseif(is_array($fcontents)){
        foreach ( $fcontents as $key => $val ) {
                        $_key =         auto_charset($key,$from,$to);
            $fcontents[$_key] = auto_charset($val,$from,$to);
                        if($key != $_key ) {
                                unset($fcontents[$key]);
                        }
        }
        return $fcontents;
    }
    elseif(is_object($fcontents)) {
                $vars = get_object_vars($fcontents);
        foreach($vars as $key=>$val) {
            $fcontents->$key = auto_charset($val,$from,$to);
        }
        return $fcontents;
    }
    else{
        return $fcontents;
    }
}
http://www.bkjia.com/phpjc/631381.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/631381.htmltecharticle编码转换 这段代码是thinkphp框架中的,感觉很常用,所以单独拎出来,大家共同学习一下。 function auto_charset($fcontents,$from='',$to='') { if( str...
该用户其它信息

VIP推荐

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