【问题标题】:How can I convert \ud83d\ude1b to show emoji?如何将 \ud83d\ude1b 转换为显示表情符号?
【发布时间】:2016-08-30 12:26:37
【问题描述】:

我正在使用 twemoji 库在网页中显示表情图像。

我将"\ud83d\ude1b \ud83d\ude1b,hi"之类的代码存储在我的数据库中的消息中,现在我需要在我的网页上显示相应的表情符号。

我正在使用 PHP 脚本将 "\u1F603" 转换为 emoji:-,

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

那我就用twemoji解析body,换成emoji图标:-

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

}

如何使用 PHP 以类似方式转换 \ud83d\ude1b \ud83d\ude1b 以在网页中显示 emoji 图标?

【问题讨论】:

  • 如果\u1F603\xF0\x9F\x98\x83一样,那么\ud83d\ude1b \ud83d\ude1b怎么也是\xF0\x9F\x98\x83
  • @Justinas,抱歉我编辑了我的问题。
  • 您提供的代码没有\u1F603转换为\xF0\x9F\x98\x83sandbox.onlinephpfunctions.com/code/…

标签: php html


【解决方案1】:

在两个 SO 问题(Thisthis)之间留出余量我设法制作了这段代码(似乎不适用于您的示例):

echo preg_replace_callback(
    "/./", // for every symbol
    function($matched) {
        return '\x'.dechex(ord($matched[0]));
    },
    json_decode('"\u1F603"') // convert Unicode to text
);

【讨论】:

  • 在我的问题中,我的数据库将数据存储在 UTF16 代理对中,例如 \ud83d\ude1b。我需要把它转换成表情符号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
  • 1970-01-01
  • 2020-10-22
  • 2019-09-03
  • 1970-01-01
  • 2015-10-18
  • 2015-01-15
相关资源
最近更新 更多