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和中国人 ...
随机推荐
- KVC实现原理简介
KVC,全称:Key-Value-Coding. KVC运用了isa-swizzling技术.isa-swizzling就是类型混合指针机制.KVC主要通过isa-swizzling来实现其内部定位查 ...
- Android源码分析之SparseArray
本来接下来应该分析MessageQueue了,可是我这几天正好在实际开发中又再次用到了SparseArray(之前有用到过一次,那次只是 大概浏览了下源码,没做深入研究),于是在兴趣的推动下,花了些时 ...
- Java 参数传递都是值传递
Java 参数传递都是值传递,验证代码如下 public class ParamTransferTest { public static void swap(int a, int b) { int t ...
- iOS网络-04-大文件下载
大文件下载注意事项 若不对下载的文件进行转存,会造成内存消耗急剧升高,甚至耗尽内存资源,造成程序终止. 在文件下载过程中通常会出现中途停止的状况,若不做处理,就要重新开始下载,浪费流量. 大文件下载的 ...
- Objective-C之Category的使用
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 使用Apache Tomcat Maven插件部署运行 Web 项目
什么是Apache Tomcat Maven Plugin? Maven Plugin 是Apache Tomcat 提供的一个Maven插件,它可以在你没有tomcat容器时将任何一个war项目文件 ...
- Android精品开源整理
一.兼容类库 ActionBarSherlock : Action Bar是Android 3.0后才开始支持的,ActionBarSherlock是让Action Bar功能支持2.X后的所有平台, ...
- 不用synchronized块的话如何实现一个原子的i++?
上周被问到这个问题,没想出来,后来提示说concurrent包里的原子类.回来学习一下. 一.何谓Atomic? Atomic一词跟原子有点关系,后者曾被人认为是最小物质的单位.计算机中的Atomic ...
- HTML实体对照表
HTML开发特殊字符是没办法原样输出的,必须用到实体,为了以后查看方便,收藏一下实体对照表是必要的,另外,使用<xmp></xmp>标签可以原样输出,当然,也包括特殊字符啦! ...
- hdu 3367 Pseudoforest(最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...