将表情符号转换为十六进制代码

Mr.*_*r33 9 php converter emoji

我正在尝试检测我通过的表情符号,例如POST(来源不是必需的).

作为一个例子,我正在使用这个表情符号:✊(我希望它是可见的)

它的代码是U+270A U+1F3FE(我使用http://unicode.org/emoji/charts/full-emoji-list.html代码)

现在我用json_encode转换了表情符号,我得到: \u270a\ud83c\udffe

这里唯一相同的部分是270a.\ud83c\udffe是不等于U+1F3FE,即使我把它们加在一起(1B83A)

我怎么从✊到U+270A U+1F3FE例如php?

d3L*_*d3L 6

使用mb_convert_encoding和转换UTF-8UTF-32.然后做一些额外的格式化:

// 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
Run Code Online (Sandbox Code Playgroud)