I have an input string (URL-encoded):
%F0%9F%98%8E
which decoded is the emoji \"
<?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 😎
(note, this answer is based on a modified version of functions provided by this answer here.)