Saswat
Saswat

Reputation: 12846

How can I convert \ud83d\ude1b to show emoji?

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?

Upvotes: 3

Views: 5337

Answers (1)

Justinas
Justinas

Reputation: 43507

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
);

Upvotes: 0

Related Questions