예제 #1
0
function mb_convert_case($str, $case, $encoding = '')
{
    global $mbemu_internals;
    switch ($case) {
        case MB_CASE_UPPER:
            return mb_strtoupper($str, $encoding);
        case MB_CASE_LOWER:
            return mb_strtolower($str, $encoding);
        case MB_CASE_TITLE:
            include_once dirname(__FILE__) . '/upper.table';
            include_once dirname(__FILE__) . '/lower.table';
            $encoding = mb_detect_encoding($str, $encoding);
            $str = mb_convert_encoding($str, 'UTF-8', $encoding);
            $max = preg_match_all('/' . $mbemu_internals['regex'][4] . '/', $str, $allchars);
            // make array of chars
            $newst = '';
            $isalpha = FALSE;
            for ($i = 0; $i < $max; ++$i) {
                $val = _utf8ucs2($allchars[0][$i]);
                //get ucs2 value
                if (0x41 <= $val && $val <= 0x5a) {
                    if ($isalpha) {
                        $val += 0x20;
                        // to lower;
                    } else {
                        $isalpha = TRUE;
                    }
                    $newst .= _ucs2utf8($val);
                } elseif (0x61 <= $val && $val <= 0x7a) {
                    if (!$isalpha) {
                        $val -= 0x20;
                        // to upper
                        $isalpha = TRUE;
                    }
                    $newst .= _ucs2utf8($val);
                } elseif ($upper = $mbemu_internals['upperarray'][$val]) {
                    // this char is lower
                    if ($isalpha) {
                        $newst .= _ucs2utf8($val);
                    } else {
                        $isalpha = TRUE;
                        $newst .= _ucs2utf8($upper);
                    }
                } elseif ($lower = $mbemu_internals['lowerarray'][$val]) {
                    // this char is upper
                    if ($isalpha) {
                        $newst .= _ucs2utf8($lower);
                    } else {
                        $isalpha = TRUE;
                        $newst .= _ucs2utf8($val);
                    }
                } else {
                    $isalpha = FALSE;
                    $newst .= $allchars[0][$i];
                }
            }
            return mb_convert_encoding($newst, $encoding, 'UTF-8');
    }
}
예제 #2
0
function _euctoutf8(&$str)
{
    global $euc_match, $sjistoucs2, $_euctosjis_byte1, $_euctosjis_byte2;
    $st = '';
    $max = preg_match_all("/{$euc_match}/", $str, $allchars);
    // 文字の配列に分解
    for ($i = 0; $i < $max; ++$i) {
        $num = ord($allchars[0][$i]);
        // 各文字の1バイト目を数値として取り出す
        if ($num2 = ord($allchars[1][$i])) {
            // 2バイト目がある場合
            if ($num & 1) {
                $sjis = $_euctosjis_byte1[$num] << 8 | $_euctosjis_byte2[0][$num2];
            } else {
                $sjis = $_euctosjis_byte1[$num] << 8 | $_euctosjis_byte2[1][$num2];
            }
            $st .= _ucs2utf8($sjistoucs2[$sjis]);
        } elseif ($num3 = ord($allchars[2][$i])) {
            $st .= _ucs2utf8(0xfec0 + $num3);
        } else {
            //半角英数
            $st .= chr($num);
        }
    }
    return $st;
}