3

This function converts emoji to unicode

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

usage

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

So it returns to me U+1F600 the problem is if I add more emoji on $var it only returns the first emoji, example of return bellow:

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

returns to me U+1F6000001F600 when it should return U+1F600 U+1F600

It works fine when convert a single emoji but not working when convert multiple emojis

Otávio Barreto
  • 1,536
  • 3
  • 16
  • 35
  • It's what the function does: convert one emoji. It was clearly written with that intend. If you need a function for multiple emojis you should rewrite your code. – KIKO Software Aug 11 '19 at 05:45

2 Answers2

3

One way to do this is to iterate over each character in $var, converting it as you go. Note that to make the function more robust, you should only replace 3 leading zeros (so as not to mess up values that e.g. start with 4). That way the function will work with all characters. I've also added a check (using mb_ord) that the character needs conversion, so that it works with plain text too:

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


$var = ("xhello");
$out = '';
for ($i = 0; $i < mb_strlen($var); $i++) {
    $out .= emoji_to_unicode(mb_substr($var, $i, 1));
}
echo "$out\n";

Output:

U+1F600xU+1F600hello

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
  • what If I have text with emoj too how to avoid text to be converted? – Otávio Barreto Aug 11 '19 at 15:05
  • I got it, but text is also returning as hex I would like to keep the emoji but return the text as plain text string – Otávio Barreto Aug 11 '19 at 15:13
  • I don't know, maybe something with `preg_replace_callback` or `mb_ord` would return the emoji in unicode and keep the text as plain text https://www.php.net/manual/en/function.preg-replace-callback.php, if you found a solution, please update. – Otávio Barreto Aug 11 '19 at 15:37
  • @OtávioBarreto see my edit. That should do what you want. Sorry about the slow response, it's been nighttime here... – Nick Aug 12 '19 at 00:52
  • Thanks for the solution, It's near machine level programing, we made the complex look so simple, thanks a lot. – Otávio Barreto Aug 12 '19 at 02:40
  • How I would add the for inside the function so the function it self already would run the for loop? – Otávio Barreto Aug 12 '19 at 04:10
2
function emoji_to_unicode($emoji) {
   $emoji = mb_convert_encoding($emoji, 'UTF-32', 'UTF-8');
   $unicode = strtoupper(preg_replace("/0{3}1/"," U+1",bin2hex($emoji)));
  return $unicode;
}

$var = ("");
echo  emoji_to_unicode($var); // U+1F600 U+1F600

$var = ("");
echo  emoji_to_unicode($var); // U+1F600 U+1F600 U+1F600
Khaled Alam
  • 885
  • 7
  • 12