最新消息: 新版网站上线了!!!

php字符串编码转换函数mb_detect_encoding

$str字符串

把$beforeEncode编码转变为$afterEncode

原理:验证字符串编码是否为$beforeEncode,如果是就转变为$afterEncode,如果不是$beforeEncode编码格式,那么查询当前字符串编码格式,然后在转为$afterEncode

array("ASCII","UTF-8","GB2312","GBK","BIG5")自己可以扩充此数组中的编码


function iconvStr($str,$beforeEncode,$afterEncode){

    $postXml = '';

    $encode = mb_detect_encoding($str, array("ASCII","UTF-8","GB2312","GBK","BIG5"));

    if($encode == $beforeEncode){

        $postXml = iconv($beforeEncode,$afterEncode,$str);

    }else{

        $postXml = iconv($encode,$afterEncode,$str);

    }

    return $postXml;

}


转载请注明:谷谷点程序 » php字符串编码转换函数mb_detect_encoding