[英]PHP function unserialize stop working after charset change (from latin1 to UTF-8)
[英]Migrating data, from latin1 charset to UTF-8
我正在嘗試使用latin1
字符集從定制的CMS移動到一些魚類信息配置文件到使用UTF-8
的WordPress自定義(自定義帖子類型,具有多個元字段)數據庫。
最重要的是,舊的CMS使用一些奇數bbCode位。
基本上,我正在尋找一個能夠做到這一點的功能:
latin1_swedish_ci
collation(和latin1
charset)從我的舊數據庫中獲取信息 á
(數字和&134;
很好)。 '
和"
為HTML實體。 utf-8
charset將信息返回到我的新數據庫 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" ) );
但是,包含我認為捷克語/克羅地亞語字符的某些字段根本不顯示。
有沒有人有任何建議,我可以按照上面列出的順序成功地將信息從“舊格式”轉換為新格式?
我想說如果你想轉換所有非ASCII字符,你就不需要做任何latin1
到UTF-8
轉換。 假設您在數據上運行htmlspecialchars
或htmlentities
等函數,則所有非ASCII字符都將替換為其對應的實體代碼。
基本上,在此步驟之后,不應該有任何需要轉換為UTF-8
字符。 此外,如果你想將你的latin1
編碼字符串轉換為UTF-8
我強烈懷疑utf8_encode
會很好。
PS。 在將bbCode
轉換為HTML
我建議使用正則表達式。 例如,您可以在這樣的行中完成所有操作:
$html_data = preg_replace('/\[(/?[a-z]+)\]/i', '<$1>', $bb_code_data);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.