Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode emoji to unicode code point - PHP/JS

Let's say I have this string on PHP:

$str = '🀄️';

Or this string on JavaScript:

var str = '🀄️';

If I do a utf8_encode($str) the result is \ud83c\udc04\ufe0f, but I want it to be 1F004 or 1f004 or \u1f004 in order to look for an image file that matches that character.

I have done many many online searches looking for a way to encode it, I have found that there are many places where same terms are used for very different things, it looks like what I want to is to "encode" a string to UTF-32 code point but I really don't know how to name what I want, I just want to convert this 🀄️ into this 1f004 using PHP and/or JavaScript.

http://www.fileformat.info/info/unicode/char/1f004/index.htm

Thanks.

like image 395
Christian Recinos Avatar asked Feb 09 '23 01:02

Christian Recinos


1 Answers

JavaScript function:

function e2u(str){
    str = str.replace(/\ufe0f|\u200d/gm, ''); // strips unicode variation selector and zero-width joiner
    var i = 0, c = 0, p = 0, r = [];
    while (i < str.length){
        c = str.charCodeAt(i++);
        if (p){
            r.push((65536+(p-55296<<10)+(c-56320)).toString(16));
            p = 0;
        } else if (55296 <= c && c <= 56319){
            p = c;
        } else {
            r.push(c.toString(16));
        }
    }
    return r.join('-');
}
like image 96
Rodrigo Polo Avatar answered Feb 11 '23 16:02

Rodrigo Polo