convert emoji character to Unicode codepoint number in php

浪尽此生 提交于 2019-12-25 00:34:46

问题


I am trying to convert emoji to unicode with php , more info: https://unicode.org/emoji/charts/full-emoji-list.html

How to convert this 😃 into this U+1F603 with php?

function convert_emoji($var){

}

回答1:


The Intl extension provides a function to return the codepoint for a character. As it returns an integer, you just need to convert it to a hex string.

function emoji_to_unicode($emoji) {
    return sprintf('U+%X', IntlChar::ord($emoji));
}



回答2:


I found a simple way to solve, so I will answer my own question, but if somebody would like to improve this function, would be cool.

<?php

function emoji_to_unicode($emoji) {
   $emoji = mb_convert_encoding($emoji, 'UTF-32', 'UTF-8');
   $unicode = strtoupper(preg_replace("/^[0]+/","U+",bin2hex($emoji)));
   return $unicode;
}

$var = "😀";
echo emoji_to_unicode($var);


?>


来源:https://stackoverflow.com/questions/57447226/convert-emoji-character-to-unicode-codepoint-number-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!