discuz编码转换UTF8与GBK互转完美适合Discuz3.x系列
“ Discuz系列对于设置项中数据类型(比如版块设置)采用序列化方式(serialize)保存,读取时使用unserialize()反序列化字符串为数组
序列化这种方式有个致命的缺点:字符集会影响序列化结果
对于这个数组序列化
$s = [1, '站长网'];
echo serialize($s);
复制代码
UTF-8环境下结果为
a:3:{i:0;i:1;i:1;s:15:"站长网";i:2;s:24;}
GB2312环境下结果为
a:3:{i:0;i:1;i:1;s:10:"站长网";i:2;s:24;}
注意看 “站长网”前面的数字,UTF-8下面为15,GB下面为10. 这是因为在UTF-8字符集中中文字长3-4个字节,而在GB/GBK中中日韩问字长都固定为2个字节。至于英文在两种字符集中都占1字节所以序列化长度是固定的
如此不同的序列化结果产生的兼容性在于不同字符集的discuz版本的数据库中保存的设置项目值是不相同的——在进行字符集版本转移时无法直接导出数据库为目的版本字符集然后再导入。必须对数组类型的设置项进行单独反序列化再使用iconv函数进行字符集转换然后才能序列化入库。遇上数组中还嵌套有数组就更加麻烦了。而如果数组中保存有已经序列化后字符串,那么此数组序列化后就……想着就头大…… ”
提示是这样的,通过使用PHP的一个函数“addslashes”该函数可用于为存储在数据库中的字符串以及数据库查询语句准备合适的字符串。这样的话我们就可以把存储在数据库中的序列化数据读出再经过该函数转换成适合编码的字符串,然后把这个字符串再入库。
在网上我也找到了写好的PHP脚本,这个脚本是用来转换uchome的,不过没关系,虽然我的php学的不是太好,但多花点时间还是能做好的。
通过对数据库字段的逐个查看,找到了存放序列化数据的很多个字段。下面是脚本内容分享给大家,希望对你有用:
/**
* 序列化整理脚本,适合uchome2.0、discuz! X2.0、discuz! X3.1,数据库转码后在脚本运行之前请不要运行网站程序(因为网站程序会改写序列化数据),建议此脚本在数据库转换完成后立即执行。
* 此脚本放置与网站根目录,如果放置其他目录请注意修改脚本中config配置文件路径。
**/
$msg = "<a href='serialize.php?step=convert&type=x2'>Discuz! X2 序列化整理</a>";
$msg .= "<br/><a href='serialize.php?step=convert&type=x31'>Discuz! X3.1 序列化整理</a>";
$msg .= "<br/><a href='serialize.php?step=convert&type=uch2'>UCHOME 2.0 序列化整理</a>";
show_msg($msg);
} elseif($step == 'convert') {
$type = $_GET['type'];
if($type == 'x2' || $type == 'x31') {
require './config/config_global.php'; // config配置文件路径请确保能连接到目标数据库
$dbserver = $_config['db']['1']['dbhost'];
$dbusername = $_config['db']['1']['dbuser'];
$dbpassword = $_config['db']['1']['dbpw'];
$database = $_config['db']['1']['dbname'];
$dbcharset = $_config['db']['1']['dbcharset'];
} elseif ($type == 'uch2') {
require '.config.php';
$dbserver = $_SC['dbhost'];
$dbusername = $_SC['dbuser'];
$dbpassword = $_SC['dbpw'];
$database = $_SC['dbname'];
$dbcharset = $_SC['dbcharset'];
}
if($dbcharset == 'gbk')
$tocharset = 'utf8';
else
$tocharset = 'gbk';
$limit = 100;
$nextid = 0;
$start = !empty($_GET['start']) ? $_GET['start'] : 0;
$tid = !empty($_GET['tid']) ? $_GET['tid'] : 0;
$arr = getlistarray($type);
$field = $arr[intval($tid)];
$stable = $field[0];
$sfield = $field[1];
$sid = $field[2];
$special = $field[3];
$mysql_conn = @mysql_connect("$dbserver", "$dbusername", "$dbpassword") or die("Mysql connect is error.");
mysql_select_db($database, $mysql_conn);
mysql_query('set names '.$dbcharset);
if($special) {
$sql = "SELECT $sfield, $sid FROM $stable WHERE $sid > $start ORDER BY $sid ASC LIMIT $limit";
} else {
$sql = "SELECT $sfield, $sid FROM $stable";
}
$query = mysql_query($sql);
while($values = mysql_fetch_array($query)) {
if($special)
$nextid = $values[$sid];
else
$nextid = 0;
$data = $values[$sfield];
$id = $values[$sid];
$data = preg_replace_callback('/s:([0-9]+?):"([\s\S]*?)";/','_serialize',$data);
$data = addslashes($data);
mysql_query("UPDATE `$stable` SET `$sfield` = '$data' WHERE `$sid` = '$id'", $mysql_conn);
}
if($nextid)
{
show_msg($stable." $sid > $nextid", "serialize.php?step=convert&type=$type&tid=$tid&start=$nextid");
}
else
{
$tid++;
if($tid < count($arr))
show_msg($stable." $sid > $nextid", "serialize.php?step=convert&type=$type&tid=$tid&start=0");
else
show_msg('转换结束', "serialize.php?step=end");
}
mysql_close($mysql_conn);
} elseif( $step == 'end') {
show_msg('整理结束');
}
$l = strlen($str[2]);
return 's:'.$l.':"'.$str[2].'";';
}
if(!empty($url_forward)) {
$message = "<a href=\"$url_forward\">$message (跳转中...)</a><script>setTimeout(\"window.location.href ='$url_forward';\", $time);</script>";
}
print<<<END
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>序列化整理工具</title>
<body>
<table>
<tr><td>$message</td></tr>
</table>
</body>
</html>
END;
}
//以下为需要整理的数据库字段。discuz! x3.1数据库中包含ucenter数据整理,如果你使用的是独立ucenter请在ucenter里(根据自身情况修改表名)再运行一次。
function getlistarray($type) {
if($type == 'uch2') {
$list = array(
array('uchome_data', 'datavalue', 'var', FALSE),
array('uchome_blogfield', 'tag', 'blogid', TRUE),
array('uchome_feed', 'body_template', 'feedid', TRUE),
array('uchome_feed', 'body_data', 'feedid', TRUE),
array('uchome_report', 'uids', 'rid', TRUE),
array('uchome_share', 'body_data', 'sid', TRUE),
array('uchome_userblock', 'blockinfo', 'bid', TRUE),
);
} elseif($type == 'x2') {
$list = array(
array('pre_common_setting','svalue', 'skey', FALSE),
array('pre_forum_grouplevel','creditspolicy','levelid', TRUE),
array('pre_forum_grouplevel','postpolicy','levelid', TRUE),
array('pre_forum_grouplevel','specialswitch','levelid', TRUE),
array('pre_common_advertisement','parameters','advid', TRUE),
array('pre_common_plugin','modules','pluginid', TRUE),
array('pre_common_block','param','bid', TRUE),
array('pre_common_block_item','fields','itemid', TRUE),
array('pre_common_block_style','template','styleid', TRUE),
array('pre_common_diy_data','diycontent','targettplname', TRUE),
array('pre_common_member_field_forum','groups','uid', TRUE),
array('pre_common_member_stat_search','condition','optionid', TRUE),
array('pre_common_syscache','data','cname', TRUE),
);
} elseif($type == 'x31') {
$list = array(
array('pre_common_setting','svalue', 'skey', FALSE),
array('pre_forum_grouplevel','creditspolicy','levelid', TRUE),
array('pre_forum_grouplevel','postpolicy','levelid', TRUE),
array('pre_forum_grouplevel','specialswitch','levelid', TRUE),
array('pre_common_advertisement','parameters','advid', TRUE),
array('pre_common_plugin','modules','pluginid', TRUE),
array('pre_common_block','param','bid', TRUE),
array('pre_common_block','blockstyle','bid', TRUE),
array('pre_common_block_item','fields','itemid', TRUE),
array('pre_common_block_style','template','styleid', TRUE),
array('pre_common_diy_data','diycontent','targettplname', TRUE),
array('pre_common_member_field_forum','groups','uid', TRUE),
array('pre_common_member_field_home','blockposition','uid', TRUE),
array('pre_common_member_field_home','privacy','uid', TRUE),
array('pre_common_member_field_home','acceptemail','uid', TRUE),
array('pre_common_member_field_home','magicgift','uid', TRUE),
array('pre_common_member_verify_info','field','vid', TRUE),
array('pre_common_patch','rule','serial', TRUE),
array('pre_common_member_stat_search','condition','optionid', TRUE),
array('pre_common_plugin','modules','pluginid', TRUE),
array('pre_common_member_newprompt','data','uid', TRUE),
array('pre_forum_activity','ufield','tid', TRUE),
array('pre_forum_forumfield','creditspolicy ','fid', TRUE),
array('pre_forum_activity','formulaperm','fid', TRUE),
array('pre_forum_activity','moderators','fid', TRUE),
array('pre_forum_activity','modrecommend','fid', TRUE),
array('pre_forum_activity','extra','fid', TRUE),
array('pre_forum_groupfield','data','fid', TRUE),
array('pre_forum_medal','permission','medalid', TRUE),
array('pre_forum_spacecache','value','uid', TRUE),
array('pre_home_feed','title_data','feedid', TRUE),
array('pre_home_feed','body_data','feedid', TRUE),
array('pre_home_share','body_data','sid', TRUE),
array('pre_ucenter_applications','extra','appid', TRUE),
array('pre_ucenter_pm_list','lastmessage','plid', TRUE),
);
}
return $list;
}
?>
保存红色代码线中间的代码另存为serialize.php然后上传到网站根目录选择自己的网站版本进行整合,到此问题解决。
discuz编码转换UTF8与GBK互转完美适合Discuz3.x系列的更多相关文章
- 理解perl的编码转换——utf8以及乱码
工作需要,闲暇之余,仔细研究了一下脚本乱码的问题 1. vim新建的文件 1)在linux命令行 vim命令建立的文件,如果内容中不出现中文,默认是ASCII.那么用notepad++打开的时候,就是 ...
- Linux字符编码转换 UTF8转GB3212
在LINUX上进行编码转换时,既可以利用iconv函数族编程实现,也可以利用iconv命令来实现,只不过后者是针对文件的,即将指定文件从一种编码转换为另一种编码. 一.利用iconv函数族进行编 ...
- 黄聪:C#中WebClient自动判断编码是UTF-8还是GBK,并且有超时判断功能
public class WebDownload : WebClient { private int _timeout; /// <summary> /// 超时时间(毫秒) /// &l ...
- 一文读懂所有的编码方式(UTF-8、GBK、Unicode、宽字节...)
编码方式就分两类:ANSI编码.Unicode编码.这两类编码都兼容ASC码. ------------------------------------------------------------ ...
- C# 编码转换 UTF8转GB2312 GB2312转UTF8
/// <summary> /// GB2312转换成UTF8 /// </summary> /// <param name="text">&l ...
- 使用MultiByteToWideChar转换UTF8为GBK(UTF8在Windows的代码页是CP_UTF8)
两个使用的函数: 1,UTF8转化为Unicode,inline为了编译后更快运行,老用到了,返回字符串为了使用链式表达式 inline WCHAR *UTF8ToUnicode(const cha ...
- 字符编码之间的转换 utf-8 , gbk等,(解决中文字符串乱码)
目录 1.背景. 2.编码的理解 3.编码之间的相互转化 4. str类型说明 5. 可以使用的编码类型 6.参考文章 1.背景 Python中与其他程序进行交互时,如果存在字符串交互,特别是字符串中 ...
- 关于utf8 unicode gbk 编码乱码汇总
首先从一个问题说起: 插入一个中文到blob类型(mysql编码是utf-unicode-ci). insert into blobtype(data) values('中文你好') 复制数据显示为 ...
- 关于JAVA字符编码:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换
我们最初学习计算机的时候,都学过ASCII编码. 但是为了表示各种各样的语言,在计算机技术的发展过程中,逐渐出现了很多不同标准的编码格式, 重要的有Unicode.UTF.ISO-8859-1和中国人 ...
随机推荐
- Android 在外部存储读写文件
本文主要介绍android中如何在外部存储读写数据 sd卡的路径 sdcard:2.3之前的sd卡路径 mnt/sdcard:4.3之前的sd卡路径 storage/sdcard:4.3之后的sd卡路 ...
- 【读书笔记】iOS-AppKit简介
一,IBOutlet和IBAction.它们实际上只是AppKit提供的#defines.IBOutlet的含义没有任何作用,因此将不对对它时行编译.IBAction定义为void,这意味着在AppC ...
- virt manager 提示权限不允许(ubuntu)
问题描述: 新安装virt manager 打开提示权限不允许(ubuntu 15.04); 提示检查libvirt-bin包是否安装:libvirtd服务是否已运行:当前用户是否在libvirtd组 ...
- Linux开机启动chkconfig命令详解(让MySQL、Apache开机启动)
chkconfig chkconfig在命令行操作时会经常用到.它可以方便地设置和查询不同运行级上的系统服务.这个可要好好掌握,用熟练之后,就可以轻轻松松的管理好你的启动服务了. 注:谨记chkcon ...
- nodeJS创建工程
转http://blog.csdn.net/i348018533/article/details/47258449 设置镜像地址 1.通过config命令 npm config set registr ...
- Effective Java 74 Implement Serializable judiciously
Disadvantage of Serializable A major cost of implementing Serializable is that it decreases the flex ...
- 一次由于字符集问题引发的MySQL主从同步不一致问题追查
近期业务准备上线一个新功能,灌入数据之后突然发现主从同步停止,报错如下: Error 'Duplicate entry '66310984-2014-04-18 00:00:00--122815.sh ...
- 在Ubuntu Server下搭建LAMP环境学习记录
更新于2015/6/16日,因图片地址失效,请在此地址查看:http://note.youdao.com/share/?id=1c249ae6dc6150cbf692adec67b23a33& ...
- IE6-8中Date不支持toISOString方法
Date.prototype.toISOString方法是在ES5里添加的,ES3文档中没有,如下 这个方法在IE6/7/8中不支持,可按下面方式修复下 if (!Date.prototype.toI ...
- linux netstat 命令简解
Netstat 简介: Netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告.常见参数-a (all)显示所有选项,默认不显示LISTEN相 ...