/**
 * 转换编码,通常是转换为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;
}

相关文章:

  • 2022-12-23
  • 2021-11-30
  • 2021-09-12
  • 2021-12-19
  • 2022-01-05
  • 2022-12-23
猜你喜欢
  • 2021-04-11
  • 2022-02-06
  • 2021-10-11
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
相关资源
相似解决方案