CamaroSS
CamaroSS

Reputation: 493

UTF-8 symbols and cp1251 site

There is a bunch of old sites created with cp1251 charset. I've been asked to enhance its functionality by implementing support for non-cp1251 symbols for user input. It's a bit too late to convert everything to utf-8, because there is lots of old undocumented stuff relying on old charset, so I came up with an idea to convert all the "non-standard" symbols to HTML-entities. There is a class I've written for this task. It converts UTF-8 to cp1251 plus HTML-entities and vice versa. What do you think about it? What problems can potentially appear after applying this to the input? Or is there any better way?

class UTFire
{
    /*
    * This will exclude cp1251 symbols from encoding
    */
    static $convmap = array(
                        0x0080, 0x009f, 0, 0xffff,
                        0x00a1, 0x00a3, 0, 0xffff,
                        0x00a5, 0x00a5, 0, 0xffff,
                        0x00a8, 0x00a8, 0, 0xffff,
                        0x00aa, 0x00aa, 0, 0xffff,
                        0x00af, 0x00af, 0, 0xffff,
                        0x00b2, 0x00b4, 0, 0xffff,
                        0x00b8, 0x00ba, 0, 0xffff,
                        0x00bc, 0x0400, 0, 0xffff,
                        0x040d, 0x040d, 0, 0xffff,
                        0x0450, 0x0450, 0, 0xffff,
                        0x045d, 0x045d, 0, 0xffff,
                        0x0460, 0x048f, 0, 0xffff,
                        0x0492, 0x2012, 0, 0xffff,
                        0x2015, 0x2017, 0, 0xffff,
                        0x201b, 0x201b, 0, 0xffff,
                        0x201f, 0x201f, 0, 0xffff,
                        0x2023, 0x2025, 0, 0xffff,
                        0x2027, 0x202f, 0, 0xffff,
                        0x2031, 0x2038, 0, 0xffff,
                        0x203b, 0x20ab, 0, 0xffff,
                        0x20ad, 0x2115, 0, 0xffff,
                        0x2117, 0x2121, 0, 0xffff,
                        0x2123, 0xffff, 0, 0xffff,
                       );
    // Detect if input contains UTF-8 chars
    static function isUTF8($str) {
            return preg_match('//u', $str);
    }

    // Forward conversion
    static function fwd($str) {
            if(static::isUTF8($str)) {
                   $str = mb_encode_numericentity($str, static::$convmap, 'UTF-8');
                   $str = iconv('UTF-8', 'windows-1251//IGNORE', $str);
            }
            return $str;
    }

    // Backward conversion
    static function bck($str) {
            if(!static::isUTF8($str)) {
                   $str = iconv('windows-1251', 'UTF-8//IGNORE', $str);
                   $str = mb_decode_numericentity($str, static::$convmap, 'UTF-8');
            }
            return $str;
    }
}

Upvotes: 1

Views: 608

Answers (0)

Related Questions