首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP to 1252/windows-1252转换为UTF-8

PHP to 1252/windows-1252转换为UTF-8
EN

Stack Overflow用户
提问于 2014-03-27 09:23:25
回答 1查看 16.3K关注 0票数 5

我正在尝试将我们的数据库从latin1转换到UTF-8。不幸的是,我无法进行大规模的单次切换,因为应用程序需要保持在线,而且我们有700‘t的数据库需要转换。

因此,我试图利用mysql的一些黑客,将表转换为UTF-8,而不是数据。我希望实时读取、转换和替换数据。(如果愿意的话,可以进行JIT转换)

我们的php应用程序目前使用所有的默认设置,因此它使用latin1字符集连接到mysql,并删除以latin1编码的UTF-8数据。当您使用latin1查看数据时,UTF-8字符将如预期的那样显示。当你用UTF-8查看数据时,事情就变得混乱起来。

因此,我建议强制将mysql字符集设置为UTF-8,然后在必要时对数据进行公正及时的转换。现在,作为to 1252/windows-1252是UTF-8的子集,检测to 1252/windows-1252编码并不是那么直接(据我所见)。

我编写了以下代码,试图检测as 1252/windows-1252编码,并根据需要进行转换。它还应该检测正确编码的UTF-8字符,什么也不做.

代码语言:javascript
复制
$a = 'Card☃'; //cp1252 encoded
$a_test = '☃'.$a; //add known UTF8 character
$c = mb_convert_encoding($a_test, 'cp1252', 'UTF-8');
// attempt to detect known utf8 character after conversion
if (mb_strpos($c, '☃') === false) {
    // not found, original string was not cp1252 encoded, so print
    var_dump($a);
} else {
    // found, original string was cp1252 encoded, remove test character and print
    // This case runs
    $c = mb_strcut($c, 1);
    var_dump($c);
}

$a = 'COD☃'; //proper UTF8 encoded
$a_test = '☃'.$a; //add known UTF8 character
$c = mb_convert_encoding($a_test, 'cp1252', 'UTF-8');
// attempt to detect known utf8 character after conversion
if (mb_strpos($c, '☃') === false) {
    // not found, original string was not cp1252 encoded, so print
    // This case runs
    var_dump($a);
} else {
    // found, original string was cp1252 encoded, remove test character and print
    $c = mb_strcut($c, 1);
    var_dump($c);
}

运行此代码的输出是:

代码语言:javascript
复制
string 'Card☃' (length=7)
string 'COD☃' (length=6)

我知道,在数据库中的所有字符串上运行这个程序将对性能产生影响,但如果我能够在完全切换所有内容之前进行JIT转换,这对我来说是值得的。

有没有人对如何优化这个问题有任何建议?

EN

Stack Overflow用户

发布于 2014-04-23 16:22:07

首先,Windows1252是而不是,是UTF-8的一个子集.你可以说ASCII是UTF-8的一个子集,但这通常是一场意识形态上的争论。

其次,不可能同时处理字符串中的CP1252和UTF-8“字符”(实际上,对于CP1252来说,它是一个字节,而对于Unicode来说,它是一个代码点)。您可以尝试将其读取为CP1252,并将所有Unicode字符视为单个字节,也可以将其作为UTF-8读取,从而删除任何无效的字节序列(如果CP1252字符与Unicode代码点匹配,则创建随机字符)。您是而不是,使用$c = mb_strcut($c, 1);删除测试字符,您正在删除mb_convert_encoding创建的问号,因为它无法将该Unicode字符转换为CP1252字符。

第三,您应该从不转换字符串,然后在事实之后尝试确定编码。在转换第二个测试字符串之后,它就是?COD?。没有理由检查其中是否存在Unicode字符,因为您将其转换为CP1252。里面不可能有Unicode字符。作为程序员,您必须知道输出是什么。

唯一的解决方案是检查字符串是否为CP1252,将违规字符转换为占位符,然后将该字符串转换为Unicode:

代码语言:javascript
复制
function convert_cp1252_to_utf8($input, $default = '', $replace = array()) {
    if ($input === null || $input == '') {
        return $default;
    }

    // https://en.wikipedia.org/wiki/UTF-8
    // https://en.wikipedia.org/wiki/ISO/IEC_8859-1
    // https://en.wikipedia.org/wiki/Windows-1252
    // http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
    $encoding = mb_detect_encoding($input, array('Windows-1252', 'ISO-8859-1'), true);
    if ($encoding == 'ISO-8859-1' || $encoding == 'Windows-1252') {
        /*
         * Use the search/replace arrays if a character needs to be replaced with
         * something other than its Unicode equivalent.
         */ 

        /*$replace = array(
            128 => "€",      // http://www.fileformat.info/info/unicode/char/20AC/index.htm EURO SIGN
            129 => "",              // UNDEFINED
            130 => "‚",      // http://www.fileformat.info/info/unicode/char/201A/index.htm SINGLE LOW-9 QUOTATION MARK
            131 => "ƒ",      // http://www.fileformat.info/info/unicode/char/0192/index.htm LATIN SMALL LETTER F WITH HOOK
            132 => "„",      // http://www.fileformat.info/info/unicode/char/201e/index.htm DOUBLE LOW-9 QUOTATION MARK
            133 => "…",      // http://www.fileformat.info/info/unicode/char/2026/index.htm HORIZONTAL ELLIPSIS
            134 => "†",      // http://www.fileformat.info/info/unicode/char/2020/index.htm DAGGER
            135 => "‡",      // http://www.fileformat.info/info/unicode/char/2021/index.htm DOUBLE DAGGER
            136 => "ˆ",      // http://www.fileformat.info/info/unicode/char/02c6/index.htm MODIFIER LETTER CIRCUMFLEX ACCENT
            137 => "‰",      // http://www.fileformat.info/info/unicode/char/2030/index.htm PER MILLE SIGN
            138 => "Š",      // http://www.fileformat.info/info/unicode/char/0160/index.htm LATIN CAPITAL LETTER S WITH CARON
            139 => "‹",      // http://www.fileformat.info/info/unicode/char/2039/index.htm SINGLE LEFT-POINTING ANGLE QUOTATION MARK
            140 => "Œ",      // http://www.fileformat.info/info/unicode/char/0152/index.htm LATIN CAPITAL LIGATURE OE
            141 => "",              // UNDEFINED
            142 => "Ž",      // http://www.fileformat.info/info/unicode/char/017d/index.htm LATIN CAPITAL LETTER Z WITH CARON 
            143 => "",              // UNDEFINED
            144 => "",              // UNDEFINED
            145 => "‘",      // http://www.fileformat.info/info/unicode/char/2018/index.htm LEFT SINGLE QUOTATION MARK 
            146 => "’",      // http://www.fileformat.info/info/unicode/char/2019/index.htm RIGHT SINGLE QUOTATION MARK
            147 => "“",      // http://www.fileformat.info/info/unicode/char/201c/index.htm LEFT DOUBLE QUOTATION MARK
            148 => "”",      // http://www.fileformat.info/info/unicode/char/201d/index.htm RIGHT DOUBLE QUOTATION MARK
            149 => "•",      // http://www.fileformat.info/info/unicode/char/2022/index.htm BULLET
            150 => "–",      // http://www.fileformat.info/info/unicode/char/2013/index.htm EN DASH
            151 => "—",      // http://www.fileformat.info/info/unicode/char/2014/index.htm EM DASH
            152 => "˜",      // http://www.fileformat.info/info/unicode/char/02DC/index.htm SMALL TILDE
            153 => "™",      // http://www.fileformat.info/info/unicode/char/2122/index.htm TRADE MARK SIGN
            154 => "š",      // http://www.fileformat.info/info/unicode/char/0161/index.htm LATIN SMALL LETTER S WITH CARON
            155 => "›",      // http://www.fileformat.info/info/unicode/char/203A/index.htm SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
            156 => "œ",      // http://www.fileformat.info/info/unicode/char/0153/index.htm LATIN SMALL LIGATURE OE
            157 => "",              // UNDEFINED
            158 => "ž",      // http://www.fileformat.info/info/unicode/char/017E/index.htm LATIN SMALL LETTER Z WITH CARON
            159 => "Ÿ",      // http://www.fileformat.info/info/unicode/char/0178/index.htm LATIN CAPITAL LETTER Y WITH DIAERESIS
        );*/

        if (count($replace) != 0) {
            $find = array();
            foreach (array_keys($replace) as $key) {
                $find[] = chr($key);
            }
            $input = str_replace($find, array_values($replace), $input);
        }
        /*
         * Because ISO-8859-1 and CP1252 are identical except for 0x80 through 0x9F
         * and control characters, always convert from Windows-1252 to UTF-8.
         */
        $input = iconv('Windows-1252', 'UTF-8//IGNORE', $input);
        if (count($replace) != 0) {
            $input = html_entity_decode($input);
        }
    }
    return $input;
}

诀窍是,您必须检查ISO-8859-1CP1252,因为它们非常相似。在玩了几个小时这个函数之后,我发现这是一种很难的方法,结果this answer救了我一命。如果你觉得这个函数有用的话,去+1这个答案。

基本上,这个函数用表示Unicode字符的HTML替换了所有坏的CP1252字节。然后,我们将字符串从ISO-8859-1/CP1252转换为UTF-8,而我们的新Unicode字符不会因为它们是简单的ASCII字符而损坏。最后,我们解码HTML实体,最后有一个100%的Unicode字符串。

票数 18
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22683132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档