Javascript 将表情符号编码为unicode代码点-PHP/JS

Javascript 将表情符号编码为unicode代码点-PHP/JS,javascript,php,unicode,encoding,Javascript,Php,Unicode,Encoding,假设我在PHP上有一个字符串: $str = 'You want get the unicode code point from a stream of byte, so utf8_encode won't help. I've found an implementation here. function utf8_to_unicode($c) { $ord0 = ord($c{0}); if ($ord0>=0 && $ord0<=127) retur

假设我在PHP上有一个字符串:

$str = 'You want get the unicode code point from a stream of byte, so utf8_encode won't help. I've found an implementation here.

function utf8_to_unicode($c)
{
    $ord0 = ord($c{0}); if ($ord0>=0   && $ord0<=127) return $ord0;
    $ord1 = ord($c{1}); if ($ord0>=192 && $ord0<=223) return ($ord0-192)*64 + ($ord1-128);
    $ord2 = ord($c{2}); if ($ord0>=224 && $ord0<=239) return ($ord0-224)*4096 + ($ord1-128)*64 + ($ord2-128);
    $ord3 = ord($c{3}); if ($ord0>=240 && $ord0<=247) return ($ord0-240)*262144 + ($ord1-128)*4096 + ($ord2-128)*64 + ($ord3-128);
    return false;
}

var_dump( dechex(utf8_to_unicode('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('-');
}

$str='您希望从字节流中获取unicode代码点,因此utf8\u编码没有帮助。我找到了一个实现

函数utf8到unicode($c)
{
$ord0=ord($c{0});如果($ord0>=0&&$ord0=192&&$ord0=224&&$ord0=240&&$ord0JavaScript函数:

功能e2u(str){
str=str.replace(//\ufe0f |\u200d/gm',);//带unicode变体选择器和零宽度连接符
变量i=0,c=0,p=0,r=[];
而(ir、 push((65536+)(p-55296非常感谢,我将把它移植到JavaScript,你太棒了!你可以在JS:中使用这个库,并像这样运行:
jsesc()