Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert emoji character to Unicode codepoint number in php

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){

}
like image 668
Otávio Barreto Avatar asked Oct 18 '25 00:10

Otávio Barreto


1 Answers

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);


?>
like image 97
Otávio Barreto Avatar answered Oct 20 '25 13:10

Otávio Barreto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!