[php]在*nix正确的转码
在php中把gbk(gb2312)或者big5码转成utf-8码是比较常用的操作。有两个方法可以实现下面的转换。优先使用mb_convert_encoding,转换的效果比较好,其次是iconv,加上//TRANSLIT保证在原编码数据出错时能够忽略错误。
以下两个函数需要mb_string 或者iconv库支持,如果都不被支持,只能使用php加载编码转换表来支持编码转换,效率比较低下。
function CharsetFromTo( $sText, $sCharsetFrom, $sCharsetTo ) {
if ( function_exists( 'mb_convert_encoding' ) ) {
return mb_convert_encoding( $sText, $sCharsetTo, $sCharsetFrom );
}
else if ( function_exists( 'iconv' ) ){
return iconv( $sCharsetFrom, $sCharsetTo . '//TRANSLIT', $sText ) ;
}
else {
//to throw an exception or die
}
}
页:
[1]