我试图从一个定制的CMS使用latin1
字符集将一些鱼物种信息配置文件移至使用UTF-8
的WordPress自定义(自定义帖子类型,具有多个元字段)数据库。将数据从latin1字符集迁移到UTF-8
最重要的是,旧的CMS使用一些奇数的bbCode位。
基本上,我正在寻找一个功能,将做到这一点:
- 从我的旧数据库中获取信息与
latin1_swedish_ci
整理(和latin1
字符集) - 转换所有非标准的字符(我们拥有包括克罗地亚语,捷克语,西班牙语,法语和德语在内的各种语言的字符到HTML实体,如
á
(数字如&134;
也很好)。 - 转换所有BB代码(见下文),以HTML
- 转换
'
和"
为HTML实体 - 返回与
utf-8
字符集的信息,我的新的数据库
BB代码to
和from
是:
$search = array('[i]', '[/i]', '[b]', '[/b]', '[pl]', '[/pl]');
$replace = array('<i>', '</i>', '<strong>', '</strong>', '', '');
,我到目前为止已经试过的功能是:
$search = array('[i]', '[/i]', '[b]', '[/b]', '[pl]', '[/pl]');
$replace = array('<i>', '</i>', '<strong>', '</strong>', '', '');
function _convert($content) {
if(!mb_check_encoding($content, 'UTF-8')
OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32'))) {
$content = mb_convert_encoding($content, 'UTF-8');
if (mb_check_encoding($content, 'UTF-8')) {
return $content;
} else {
echo "<p>Couldn't convert to UTF-8.</p>";
}
}
}
function _clean($content) {
$content = _convert($content);
/* edited out because otherwise all HTML appears as <html> rather than <html>
//$content = htmlentities($content, ENT_QUOTES, "UTF-8");
$content = str_replace($search, $replace, $content);
return $content;
}
但是,这阻止了一些字段被导入到新的数据库,并且不会取代bbCode。
如果我使用下面的代码,它主要作品:
$var = str_replace($search, $replace, htmlentities($row["var"], ENT_QUOTES, "UTF-8"));
然而,含有一定的领域是什么,我认为是捷克/克罗地亚的字符不会出现在所有。
有没有人有任何建议,我可以按照上面列出的顺序成功地将信息从“旧格式”转换为新的?
从latin1转换到'&134;'并不完全是“使用UTF-8”。为什么这个而不是真正的UTF-8? – deceze 2012-02-01 00:50:00
我必须承认,我并不知道任何关于charsets的知识或了解他们的工作方式。这篇文章背后的“关键事实”基本上是这样的:当我迁移到我的新数据库时,某些字段没有被WordPress识别。当我减少这个问题时,我发现它是导致问题的非标准字符。进一步研究,我推测 - 在这篇文章的指导下[http://wordpress.stackexchange.com/questions/40770/problem-with-serialized-arrays-in-custom-meta] - 这将是因为字符集差异。 – dunc 2012-02-01 11:10:00