23
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPで Unicode のコードポイントから文字列を出力する

Last updated at Posted at 2014-09-10

サンプルコード

この方法なら、携帯電話向けの絵文字も出力可能

// U+1F37A の場合
$code = '1F37A';
$bin = pack('H*', (str_repeat('0', 8 - strlen($code)) . $code));
// PHP5.4以降の場合は hex2bin() 関数が使用可能
// $bin = hex2bin(str_repeat('0', 8 - strlen($code)) . $code);

$char =  mb_convert_encoding($bin, 'UTF-8', 'UTF-32BE');
var_dump($char);

出力結果
=> string(4) ":beer:"

合成文字の場合

合成文字の場合は、 バイナリ文字列を結合すれば OK

// U+0031 U+20E3 の場合
$code = '0031';
$code2 = '20E3';
$bin = pack('H*', (str_repeat('0', 8 - strlen($code)) . $code));
$bin2 = pack('H*', (str_repeat('0', 8 - strlen($code2)) . $code2));

$char =  mb_convert_encoding($bin . $bin2, 'UTF-8', 'UTF-32BE');
var_dump($char);

出力結果
=> string(4) ":one:"

参考

23
22
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
23
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?