11

I'm trying to detect the emoji that I get through e.g. a POST (the source ist not necessary).

As an example I'm using this emoji: ✊🏾 (I hope it's visible)

The code for it is U+270A U+1F3FE (I'm using http://unicode.org/emoji/charts/full-emoji-list.html for the codes)

Now I converted the emoji with json_encode and I get: \u270a\ud83c\udffe

Here the only part that is equal is 270a. \ud83c\udffe is not equal to U+1F3FE, not even if I add them together (1B83A)

How do I get from ✊🏾 to U+270A U+1F3FE with e.g. php?

3 Answers 3

9

Use mb_convert_encoding and convert from UTF-8 to UTF-32. Then do some additional formatting:

// Strips leading zeros
// And returns str in UPPERCASE letters with a U+ prefix
function format($str) {
    $copy = false;
    $len = strlen($str);
    $res = '';

    for ($i = 0; $i < $len; ++$i) {
        $ch = $str[$i];

        if (!$copy) {
            if ($ch != '0') {
                $copy = true;
            }
            // Prevent format("0") from 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 the UTF-32 hex representation into chunks
    $hex_len = strlen($hex) / 8;
    $chunks = array();

    for ($i = 0; $i < $hex_len; ++$i) {
        $tmp = substr($hex, $i * 8, 8);

        // Format each chunk
        $chunks[$i] = format($tmp);
    }

    // Convert chunks array back to a string
    return implode($chunks, ' ');
}

echo convert_emoji('✊🏾'); // U+270A U+1F3FE
4

Simple function, inspired by @d3L answer above

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

Exmaple

emoji_to_unicode("💵");//returns U+1F4B5
2

You can do like this, consider the emoji a normal character.

$emoji = "✊🏾";

$str = str_replace('"', "", json_encode($emoji, JSON_HEX_APOS));

$myInput = $str;

$myHexString = str_replace('\\u', '', $myInput);
$myBinString = hex2bin($myHexString);

print iconv("UTF-16BE", "UTF-8", $myBinString); 
3
  • I don't want to create a list with all emojis. If there is a code, how did someone else get to it?
    – Mr.Tr33
    Nov 13, 2016 at 16:20
  • Sorry, I misunderstood your question. I guess you'll need a library for that.
    – Vinny
    Nov 13, 2016 at 17:57
  • Well that is still not that what I wrote as a final question ... you just corrected the json_encode and printed it again.
    – Mr.Tr33
    Nov 13, 2016 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.