将高代码位(>U+FFFF)编码为HTML实体 [英] Encode a high code point (> U+FFFF) to HTML entities

查看:0
本文介绍了将高代码位(>U+FFFF)编码为HTML实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入字符串(URL编码):

%F0%9F%98%8E

哪个表情符号是"😎"。

如何将其转换为HTML码😎

http://unicode.online-toolz.com/tools/unicode-html-entities-convertor.php

这个站点正在做我需要的事情。

推荐答案

<?php

function mb_ord($char, $encoding = 'UTF-8') {
    if ($encoding === 'UCS-4BE') {
        list(, $ord) = (strlen($char) === 4) ? @unpack('N', $char) : @unpack('n', $char);
        return $ord;
    } else {
        return mb_ord(mb_convert_encoding($char, 'UCS-4BE', $encoding), 'UCS-4BE');
    }
}

function mb_htmlentities($string, $hex = false, $encoding = 'UTF-8') {
    return preg_replace_callback('/[x{80}-x{10FFFF}]/u', function ($match) use ($hex) {
        return sprintf($hex ? '&#x%X;' : '&#%d;', mb_ord($match[0]));
    }, $string);
}


echo mb_htmlentities(urldecode('%F0%9F%98%8E'));

这将返回&#128526;

(请注意,此答案基于a modified version of functions provided by this answer here。)

这篇关于将高代码位(&gt;U+FFFF)编码为HTML实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆