Encode a high code point (> U+FFFF) to HTML entities

前端 未结 1 1894
孤独总比滥情好
孤独总比滥情好 2021-01-20 20:22

I have an input string (URL-encoded):

%F0%9F%98%8E

which decoded is the emoji \"

相关标签:
1条回答
  • 2021-01-20 21:19
    <?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'));
    

    This will return &#128526;

    (note, this answer is based on a modified version of functions provided by this answer here.)

    0 讨论(0)
提交回复
热议问题