テキストエディタでEUC-JPで保存してね。
サンプルを確認したサーバー環境:
・PHP Version 5.3.2-1ubuntu4.14
サンプルを確認したOSとブラウザ:
・XP で IE8、Google Chrome
・Ubuntu 10.04 で Google Chrome
<?
$chr = "";
$hex = "";
if (isset($_POST["check_char"])) {
// ポストされた文字を16進に変換
$chr = $_POST["check_char"];
$arr = unpack("C*", $chr);
$hex = "";
for ($j = 1; $j <= count($arr); $j++) {
$hex.= base_convert($arr[$j], 10, 16);
}
}
?>
<html>
<head>
<meta http-equiv="content-language" content="ja" />
<meta http-equiv="content-type" content="text/html; charset=EUC-JP" />
<title>EUC-JP</title>
</head>
<body>
<form action="" method="post" accept-charset="EUC-JP">
<p>チェックする文字:<input type="text" name="check_char" value="<?=$chr?>" /></p>
<p>ポストされた文字:<?=$hex?></p>
<p><input type="submit" value="CHECK" /></p>
</form>
</body>
</html>
「チェックする文字」に「№」と入力した結果は
【XP の結果】
ポストされた文字:ade2
【Ubuntu 10.04 の結果】
ポストされた文字:ade2
おや?XPなら「まー、そうかなー」なんだけど、Ubuntuさんまで2バイト文字でポストしちゃってるのね。
ちなみに、PHPで SJIS-win な文字(2バイト文字)を EUC-JP へ変換すると、3バイト文字に変換される。
下のサンプルを Shift_JIS(CP932)で保存して実行してみて。
<?
$chr = "";
$hex = "";
$chr = mb_convert_encoding("№", "EUC-JP", "SJIS-win");
$arr = unpack("C*", $chr);
$hex = "";
for ($j = 1; $j <= count($arr); $j++) {
$hex.= base_convert($arr[$j], 10, 16); // 8fa2f1 に変換される。
}
?>
<html>
<head>
<meta http-equiv="content-language" content="ja" />
<meta http-equiv="content-type" content="text/html; charset=Shift_JIS" />
<title>EUC-JP</title>
</head>
<body> <p>変換された文字:<?=$hex?></p> </body> </html>
理由がわからん。
EUC-JP って言ってるのにさ、2バイト文字として送信されるのは何故?