0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PHPでEUC-JPをUTF-8に変換する。はしご高(髙)

Posted at

PHPでEUC-JPをUTF-8に変換する。はしご高(髙)

  • php5.2.1以降からCP51932が使える模様
  • はしご高(髙)がうまく変換されない
  • とはいえ、CP51932では3byte文字がないらしい
// EUC-JP -> UTF8
// 1, 2byte文字は'CP51932'、3byte文字は'EUC-JP'として扱う
function convertEUCtoUTF8($text) {
    $len = strlen($text);
    $ret = '';
    $pos = 0;
    for ($i=0; $i<$len; $i++) {
        $b = substr($text, $i, 1);
        if ($b == "\x8f") {
            $ret .= @mb_convert_encoding(substr($text, $pos, $i-$pos), 'UTF-8', 'CP51932');
            $ret .= @mb_convert_encoding(substr($text, $i, 3),         'UTF-8', 'EUC-JP');
            $i += 2;
            $pos = $i + 1;
        }
    }
    if ($pos != $len) {
        $ret .= @mb_convert_encoding(substr($text, $pos, $len-$pos), 'UTF-8', 'CP51932');
    }
    return $ret;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?