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.
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('-');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With