自动判断把gbk或gb2312编码的字符串转为utf8

/**

*自动判断把gbk或gb2312编码的字符串转为utf8

*能自动判断输入字符串的编码类,如果本身是utf-8就不用转换,否则就转换为utf-8的字符串

*支持的字符编码类型是:utf-8,gbk,gb2312

*@$str:string 字符串

*/

function yang_gbk2utf8($str){

$charset = mb_detect_encoding($str,array('UTF-8','GBK','GB2312'));

$charset = strtolower($charset);

if('cp936' == $charset){

$charset='GBK';

}

if("utf-8" != $charset){

$str = iconv($charset,"UTF-8//IGNORE",$str);

}

return $str;

}


本文新知识:mb_detect_encoding

来源:http://www.jb51.net/article/42497.htm

你可能感兴趣的:(自动判断把gbk或gb2312编码的字符串转为utf8)