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和中国人 ...
随机推荐
- 深入理解Activity -动手写实例来感受Activity的启动模式
介绍 上篇提到了Activity的任务,任务栈,以及启动模式.对这些概念有了了解以后,自己写一下例子来感受一下,就当作复习和加深印象了.如果对概念不熟悉的可以参考:深入理解Activity-任务,回退 ...
- Android学习一(入门)
一.Android 系统简介 1.1.1G-4G 1G:模拟制式手机,1995年问世的第一代模拟制式手机,只能进行语音通话, 2G:手机使用GSM,CDMA(9K/s),增加了接收数据的功能 2.5G ...
- iOS NSDate、NSCalendar、NSDateComponents
时间解析(NSDate.NSCalendar.NSDateComponents): 1.使用NSCalendar和NSDateComponents解析日期,直接获取到年月日时分秒.获取到年月日时分秒其 ...
- Mac 下使用sourcetree操作git教程
SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,同时也是Mercurial和Subversion版本控制系统工具.支持创建.克隆.提交.push.pu ...
- IOS开发--仿制网易新闻
学习来源:袁峥老师的<快速集成App中顶部标题滚动条> 此次博文写的是按需求分析写代码,思路条理性杠杠的,可以提高的编码实现速度哦. 效果: 根据这个网易新闻的界面,需求分析: ...
- iOS开发网络篇—Reachability检测网络状态
前言:当应用程序需要访问网络的时候,它首先应该检查设备的网络状态,确认设备的网络环境及连接情况,并针对这些情况提醒用户做出相应的处理.最好能监听设备的网络状态的改变,当设备网络状态连接.断开时,程序也 ...
- 双系统如何删除Linux,恢复Windows从MBR引导启动?
嗯,现在愿意尝试Linux的人越来越多了.通常,如果一台电脑里已经装有了Windows,再装Linux,安装时,Linux的grub引导程 序就会覆盖掉保存在MBR当中的原来的Windows引导程序. ...
- 3.输入三个整数,xyz,最终以从小到大的方式输出。利用嵌套。
<body>请输入a的值:<input type="numbe" id="a" value=""/>请输入b的值:& ...
- PS网页设计教程XXIX——如何在PS中设计一个画廊布局
作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...
- oracle的增删改查语句
创建一个表: cteate table 表名(列1 类型, 列2 类型);查看表结构 desc表名添加一个字段 alter table 表名 add(列类型);修改字段类型 alter table 表 ...