LoginSignup
0
0

More than 1 year has passed since last update.

見えない文字が隠れているのを発見するコード

Last updated at Posted at 2022-05-30

経緯

なぜだか、PHPで文字を置換したりすると、NULL文字が出てきたりして
大手キャリアは対応してくれますが、
メール送信、LINE送信などでnull文字があると、
それ以降の文字が消えて送信される不具合が発生。
どの文字コードが原因か調べるために作った(パクった)関数です。

関数

function changeUnicode($utf8_str){
    //入力をUTF-32に変換
    $utf32_str=mb_convert_encoding($utf8_str,'UTF-32','UTF-8');

    for($i=0;$i<mb_strlen($utf32_str,'UTF-32');++$i)
        //1文字ずつ16進文字列に変換
        $result[]=bin2hex(mb_substr($utf32_str,$i,1,'UTF-32'))."|".mb_substr($utf8_str,$i,1,'UTF-8');
    return $result;
}


$str = "こんにちは!";

print_r(changeUnicode($str));

/*出力
Array
(
    [0] => 00003053|こ
    [1] => 00003093|ん
    [2] => 0000306b|に
    [3] => 00003061|ち
    [4] => 0000306f|は
    [5] => 00000021|!
)
*/

ほんとかよー

確認
Unicode一覧 3000-3FFF - Wikipedia
Unicode一覧 0000-0FFF - Wikipedia

00003053 → U+3053 → こ
00003093 → U+3093 → ん
0000306b → U+3093 → に
00003061 → U+3061 → ち
0000306f → U+3061 → は
00000021→ U+0021 → !

OK!!

参考にしたサイト

というかほぼパクリですが。

PHPにおけるUnicode文字列の正規化 (1/2):CodeZine(コードジン)

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