変数「xxx」に入れた文字列を各文字コードに変換して表示する
<?php $xxx = "あ"; $enc = array("default","UTF-8","SHIFT-JIS","EUC-JP","ASCII"); function display($str,$enc){ echo "String \"" . $str . "\" in " . $enc . ":\n"; if($enc == "default"){ echo shell_exec("echo $str | od -An -tx1"); }else{ echo shell_exec("echo $str | iconv -t $enc | od -An -tx1"); } echo "--------------------\n"; } for ($i = 0; $i < count($enc); $i++ ){ display($xxx, $enc[$i]); }
結果
String "あ" in default: e3 81 82 0a -------------------- String "あ" in UTF-8: e3 81 82 0a -------------------- String "あ" in SHIFT-JIS: 82 a0 0a -------------------- String "あ" in EUC-JP: a4 a2 0a -------------------- String "あ" in ASCII: iconv: 位置 0 に不正な入力シーケンスがあります --------------------
末尾の「0a」は改行コード「LF」を指す
shell_exec内のehcoに「-n」オプションを付けることで非表示にできる
文字コード表
UTF-8 https://seiai.ed.jp/sys/text/java/utf8table.html
Shift-JIS http://charset.7jp.net/sjis.html