3

I am using twemoji library to show emoticon image in Webpage.

I store codes like "\ud83d\ude1b \ud83d\ude1b,hi" within messages in my database, and now I need to display the corresponding emoji on my web page.

I am using PHP script to convert "\u1F603" to emoji:-,

$text = "This is fun \u1f602! \u1f1e8 ";
$html = preg_replace("/\\\\u([0-9A-F]{2,5})/i", "&#x$1;", $text);
echo $html;

Then I am using twemoji to parse the body and replace it with emoji icons:-

window.onload = function() {

  // Set the size of the rendered Emojis
  // This can be set to 16x16, 36x36, or 72x72
  twemoji.size = '16x16';

  // Parse the document body and
  // insert <img> tags in place of Unicode Emojis
  codePoint = twemoji.convert.toCodePoint($('.com-text').html());
  twemoji.parse(document.body);

}

How can I convert \ud83d\ude1b \ud83d\ude1b in similar way using PHP for displaying emoji icons in a web page?

Saswat
  • 12,320
  • 16
  • 77
  • 156
  • If `\u1F603` is same as `\xF0\x9F\x98\x83`, than how `\ud83d\ude1b \ud83d\ude1b` be also `\xF0\x9F\x98\x83`? – Justinas Aug 30 '16 at 12:36
  • @Justinas, sorry I edited my question. – Saswat Aug 30 '16 at 12:41
  • Your provided code *does not* converts `\u1F603` to `\xF0\x9F\x98\x83`: http://sandbox.onlinephpfunctions.com/code/0de4bd662dbb0f0790ff36306707eedc23b868c6 – Justinas Aug 30 '16 at 12:46

1 Answers1

0

Margin two SO questions (This and this) I manage to make this code (seems not working with your example):

echo preg_replace_callback(
    "/./", // for every symbol
    function($matched) {
        return '\x'.dechex(ord($matched[0]));
    },
    json_decode('"\u1F603"') // convert Unicode to text
);
Community
  • 1
  • 1
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • In my question, my db stores data in UTF16 surrogate pair like `\ud83d\ude1b`. I need to convert that into emoji. – Saswat Aug 30 '16 at 13:06