我开发插件习惯utf8为 标准,然而总有 客户用的 是gbk编码,如果实现兼容呢?以下为兼容笔记。 获取当前帝国 cms的编码
$ecms_config['sets']['pagechar'] 2.撤销header //一旦引入connect.php,帝国就发送了有关编码的header消息
//页面编码
if($ecms_config['sets']['setpagechar']==1)
{
if($ecms_config['sets']['pagechar']=='gb2312'||$ecms_config['sets']['pagechar']=='big5'||$ecms_config['sets']['pagechar']=='utf-8')
{
@header('Content-Type: text/html; charset='.$ecms_config['sets']['pagechar']);
}
}
// 我们在插件里,可以这样去掉他
header_remove();
@header('Content-Type: text/html; charset=utf-8'); 3. 增加一个函数
function jhc_ru($str){
global $ecms_config;
if($ecms_config['sets']['pagechar']=='utf-8')return $str;
return auto_charset($str,"UTF-8",$ecms_config['sets']['pagechar']);
}
function jhc_chu($str){
global $ecms_config;
if($ecms_config['sets']['pagechar']=='utf-8')return $str;
return auto_charset($str,$ecms_config['sets']['pagechar'],"UTF-8");
}
function auto_charset($fContents, $from='gbk', $to='utf-8') {
$from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
$to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
return $fContents;
}
if (is_string($fContents)) {
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($fContents, $to, $from);
} else if (function_exists('iconv')) {
return iconv($from, $to, $fContents);
} else {
return $fContents;
}
} else if (is_array($fContents)) {
foreach ($fContents as $key => $val) {
$_key = auto_charset($key, $from, $to);
$fContents[$_key] = auto_charset($val, $from, $to);
if ($key != $_key)
unset($fContents[$key]);
}
return $fContents;
}
else {
return $fContents;
}
} 这样,从数据库读出来数据后,做个编码转换。要把数据写到数据库之前,也做个编码转换。 这样处理后,就可以实现,两种编码的兼容。注意,是以utf-8为准。
|