我的目标是检查消息是否有表情符号,如果是,我将转换它们。我很难找出字符串中有多少个表情符号。例子 :$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); } }我将不胜感激任何帮助或建议,谢谢。
1 回答

素胚勾勒不出你
TA贡献1827条经验 获得超9个赞
我稍微更新了你的表情:
preg_match_all('/\:(.+?)(\:)/', $string, $emojis, PREG_SET_ORDER);
echo var_dump($emojis);
现在:已转义,否则可以将其视为特殊字符。
替换了*by +,这样你就不会连续匹配两个::,但至少需要一个字符。
- 1 回答
- 0 关注
- 272 浏览
添加回答
举报
0/150
提交
取消