Mr.Tr33 Mr.Tr33 - 26 days ago 9
PHP Question

convert emoji to their hex code

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: ✊

d3l d3l
Answer

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