LoginSignup
6
3

More than 5 years have passed since last update.

PHP: SJIS全角文字の後半1バイトだけを無理に変換しようとすると、次の文字を巻き込んでしまう仕様のようだ

Last updated at Posted at 2018-08-07
<?php

function sjis(string $s) {
    return mb_convert_encoding($s, 'SJIS-win', 'UTF-8');
}

function utf8(string $s) {
    return mb_convert_encoding($s, 'UTF-8', 'SJIS-win');
}

function bytes(string $s) {
    return strtr(urlencode($s), '%', 'x');
}

function strings(string $b) {
    return urldecode(strtr($b, 'x', '%'));
}

function println(string ...$x) { foreach ($x as $y) echo $y, PHP_EOL; }

ini_set('mbstring.substitute_character', 'long');

println(
    bytes(sjis("◎!")), // x81x9Dx21 (全角二重マルと半角の感嘆符)
    utf8(strings('x9Dx21')) // BAD+9D21
);

所感

6
3
1

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
6
3