php - convert emoji to their hex code -
i'm trying detect emoji through e.g. post (the source ist not necessary).
as example i'm using emoji: ✊🏾 (i hope it's visible)
the code u+270a u+1f3fe
(i'm using http://unicode.org/emoji/charts/full-emoji-list.html codes)
now converted emoji json_encode , get: \u270a\ud83c\udffe
here part equal 270a
. \ud83c\udffe
not equal u+1f3fe
, not if add them (1b83a
)
how ✊🏾 u+270a u+1f3fe
e.g. php?
use mb_convert_encoding
, convert utf-8
utf-32
. additional formatting:
// strips leading zeros // , returns str in uppercase letters u+ prefix function format($str) { $copy = false; $len = strlen($str); $res = ''; ($i = 0; $i < $len; ++$i) { $ch = $str[$i]; if (!$copy) { if ($ch != '0') { $copy = true; } // prevent format("0") returning "" else if (($i + 1) == $len) { $res = '0'; } } if ($copy) { $res .= $ch; } } return 'u+'.strtoupper($res); } function convert_emoji($emoji) { // ✊🏾 --> 0000270a0001f3fe $emoji = mb_convert_encoding($emoji, 'utf-32', 'utf-8'); $hex = bin2hex($emoji); // split utf-32 hex representation chunks $hex_len = strlen($hex) / 8; $chunks = array(); ($i = 0; $i < $hex_len; ++$i) { $tmp = substr($hex, $i * 8, 8); // format each chunk $chunks[$i] = format($tmp); } // convert chunks array string return implode($chunks, ' '); } echo convert_emoji('✊🏾'); // u+270a u+1f3fe
Comments
Post a Comment