从HTML中的Twitter呈现Twitter表情符号的最佳方法? [英] Best way to render Twitter emojis from twitter in html?
问题描述
所以我有来自twitter api查询的json对象.原始形式的文本是这样的:
So I have the json object from a query of the twitter api. In it's raw form, the text is like this:
"check out this emoji \ud83d\udc98"
我花了很多时间阅读有关unicode及其格式的信息,并且我管理了一个以json unicode作为键的库,如下所示:
I've spent a lot of time reading about unicode and it's formats, and I've managed a library that has the json unicode as the keys like this:
$emoji_dictionary = array(
'\ud83d\udd39'=> array(
'emoji-id'=> 'e-B76',
'codepoint'=> 'U+1F539',
'name'=> 'SMALL BLUE DIAMOND',
'twitter-id'=> '1f539'
),
'\ud83d\ude3f'=> array(
'emoji-id'=> 'e-34D',
'codepoint'=> 'U+1F63F',
'name'=> 'CRYING CAT FACE',
'twitter-id'=> '1f63f'
),
...
);
因此,现在我一直在像地狱一样尝试评估从twitter获得的json unicode作为字符串,然后可以将其放入该函数:
So now I've been trying like hell to evaluate the json unicode that I got from twitter as a string, which I can then throw in to this function:
function get_src($str) {
echo 'regex found:' . $str . '<br />';
return '<img class="twitter-emoji" src="https://abs.twimg.com/emoji/v1/72x72/' . $emoji_dictionary[$str]['twitter-id'] . '.png"/>';
}
哪个从Twitter返回该表情符号的图像,但是我似乎无法在PHP中正确地preg_replace
json数据.有时会出现此错误:
Which returns an image from twitter of that emoji, but I can't seem to preg_replace
the json data properly in PHP. I get this error sometimes:
preg_replace(): Compilation failed: PCRE does not support \L, \l, \N{name}, \U, or \u
我的preg_replace就像这样(请注意,这是行不通的):
My preg_replace is like this (note that this does not work):
$text = strval(json_encode($twitter_datum->text));
$pattern = "/\\\\u([a-f0-9]{4})/e";
$text = preg_replace($pattern, "get_src($1)", $text;
此模式分别捕获'd83d'和'dc98'.
This pattern grabs 'd83d' and 'dc98' separately.
我要做什么是不可能的?我只想从"check out this emoji!! \ud83d\udc98"
Is what I'm trying to do impossible? I just want to get 1f498
(from the dictionary) from "check out this emoji!! \ud83d\udc98"
推荐答案
对于任何尝试做这种事情的人,这是我所学到的:
To anyone trying to do something like this, here's what I've learned:
对json_encodeed
进行字符串操作是一个坏主意.我这样做是因为我看不到unicode表达式,而是看到了小框&因此,不知道如何评估它们.
String operations on something json_encodeed
is a bad idea. I was doing this because I couldn't see the unicode expression but little boxes instead & thus and didn't know how to evaluate them.
PHP的表情符号是此类资源的绝佳资源.它可以用<span class="xxx'></span>
交换任何出现的Unicode表情符号,其中xxx
映射到该表情符号的子图形.它所做的事情与我尝试做的事情类似,但是有两个主要区别:
Emoji for PHP is a GREAT resource for this sort of thing. It can swap out any unicode emoji occurrence with <span class="xxx'></span>
, where the xxx
maps to a sprite of that emoji. It does something similar to what I was trying to do, but for two main differences:
- 代码的正则表达式位于json_decoded实体上
- 与其将src替换为Twitter的src而不是将其替换为
<img>
,而是参考本地png转到<span>
- The code's regex is on the json_decoded entity
- Rather than replacing it with an
<img>
with an src going to twitter, this goes to<span>
with reference to a local png
我的代码现在看起来像这样,并且工作正常.唯一的问题是,如果/当添加了新表情符号时,此脚本将无法识别它们.也许到那时,表情符号会更加通用,带有完整的浏览器支持,等等.这就是我所拥有的: $ json
My code now looks like this, and it is working fine. The only issue is if/when new emojis are added, they will not be recognized by this script. Maybe at that point emojis will be a little more universal, w/ full browser support, etc. Here's what I have: $json
function twitter_chron() {
$json = get_tweets(50);
$twitter_data = json_decode($json);
include(ABSPATH . 'wp-content/themes/custom/emoji/emoji.php');
foreach($twitter_data as $twitter_datum) {
$id = $twitter_datum->id;
if (property_exists($twitter_datum, 'retweeted_status')) {
$text = 'RT: ' . $twitter_datum->retweeted_status->text;
} else {
$text = $twitter_datum->text;
}
$text = emoji_unified_to_html($text);
$text = iconv("UTF-8", "ASCII//IGNORE", $text);
insert_tweet($id, $text, $date);
}
}
emoji_unified_to_html($text)
来自emoji.php
.我还有针对推文正文运行的其他功能,用于链接,主题标签&提到了,但我认为这与特定的表情符号无关.
The emoji_unified_to_html($text)
is from the emoji.php
. I have additional functions that I run against the tweet body for links, hashtags & mentions but I figure it's not relevant to this particular issue of emojis.
希望这对某人有帮助.
这篇关于从HTML中的Twitter呈现Twitter表情符号的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!