我的目标是检查消息是否有表情符号,如果是,我将转换它们。我遇到了一个问题,无法找出字符串中有多少个表情符号。
例:
$string = ":+1::+1::+1:"; //can be any string that has :something: between ::
这里的目标是得到:+1:(所有他们),但我尝试了两种模式,我一直只得到1场比赛。
preg_match_all('/:(.*?):/', $string, $emojis, PREG_SET_ORDER); // $emojis is declared as $emojis='' at the beginning.
preg_match_all('/:(\S+):/', $string, $emojis, PREG_SET_ORDER);
获得匹配后,其余的代码
我正在这样做:
if (isset($emojis[0]))
{
$stringMap = file_get_contents("/api/common/emoji-map.json");
$jsonMap = json_decode($stringMap, true);
foreach ($emojis as $key => $value) {
$emojiColon = $value[0];
$emoji = key_exists($emojiColon, $jsonMap) ? $jsonMap[$emojiColon] : '';
$newBody = str_replace($emojiColon, $emoji, $newBody);
}
}
我将不胜感激任何帮助或建议,谢谢。
德玛西亚99