Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active September 19, 2017 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masakielastic/c2808bba448df4ff3f3110e7d3180b2d to your computer and use it in GitHub Desktop.
Save masakielastic/c2808bba448df4ff3f3110e7d3180b2d to your computer and use it in GitHub Desktop.
0x0 から 0xff の範囲のバイトに対して mb_convert_encoding が無効なバイトをどのように置き換えるのかを確認します。
<?php
$enc = "cp932";
for ($cp = 0; $cp < 0x100; $cp++) {
$c = chr($cp);
$ret = mb_convert_encoding($c, $enc, $enc);
echo
bin2hex($c), " ",
bin2hex($ret), " ",
mb_check_encoding($c, $enc) ? "true":"false",
PHP_EOL;
}
enc = "cp932"
(0..0xff).each do |cp|
c = [cp.to_s(16)].pack("H*").force_encoding(enc)
ret = c.scrub
puts "#{c.unpack("H*")[0]} #{ret.unpack("H*")[0]} #{c.valid_encoding?}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment