3

I have an input string (URL-encoded):

%F0%9F%98%8E

which decoded is the emoji "".

How can I convert this to the HTML-Code😎?

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

this site is doing exactly what I need.

1 Answers1

2
<?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.)

Community
  • 1
  • 1
Tim Groeneveld
  • 8,739
  • 3
  • 44
  • 60
  • if I pass this function a url http://example.com/abc+xyz it changes into http://example.com/abc xyz any ideas I could handle this – Abdul Mannan Feb 24 '16 at 08:16
  • 1
    @AbdulMannan you could replace do a str_replace('+', '%20'), run `mb_htmlentities` and then convert back the '%20' to '+'. You could try getting rid of the `urldecode` function (as this is what is causing the plus sign to be converted to a space) but that may cause issues with mb_htmlentities. YMMV :) – Tim Groeneveld Feb 25 '16 at 02:54
  • thanks @timgws removed url decode and it fixed the issue. – Abdul Mannan Feb 25 '16 at 10:37