`
renzhelife
  • 浏览: 669374 次
文章分类
社区版块
存档分类
最新评论

转换编码到utf-8

 
阅读更多

/**
* 转换编码,通常是转换为utf-8
*
* @param string $string 要转换的字符串
* @param string $toencode 要转换为的编码,默认为UTF-8
* @return string
*/
function convert_encoding( $string, $to_encode = 'UTF-8' ){
//当前编码
$now_encode = detect_encode( $string );

// 只有编码不同时才转换
if( strtolower( $to_encode ) != strtolower( $now_encode ) ){
$string = mb_convert_encoding( $string, $to_encode, $now_encode );
}

return $string;
}

/**
* 检测字符串当前编码
*
* @param string $string 需要检测的字符串
* @access public
* @return string 字符编码:UTF-8,...
*/
function detect_encode( $string ){
//$encodes = array( 'CP936', 'UTF-8', 'ASCII', 'GBK', 'GB2312' );
$encodes = array( 'ASCII', 'UTF-8', 'GBK', 'GB2312', 'CP936' );
$now_encode = mb_detect_encoding( $string, $encodes );
return $now_encode;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics