如何在PHP中将表情符号代码':)'转换为笑脸表情符号

时间:2018-08-22 16:07:10

标签: php

我想将表情符号代码,即“ :)”转换为。

如何转换?

目前,我正在使用p3k检测器来检测表情符号。下面是我可以检测到表情符号的代码。

  $emoji = \Emoji\detect_emoji($text);

2 个答案:

答案 0 :(得分:2)

您需要创建自己的函数进行转换(或在线搜索(如果已经有任何库在执行此操作)。

function stringToEmoji($str) {
    $emojis = [
        'o/'         => '',
        '</3'        => '',
        '<3'         => '',
        '8-D'        => '',
        '8D'         => '',
        ':-D'        => '',
        '=-3'        => '',
        '=-D'        => '',
        '=3'         => '',
        '=D'         => '',
        'B^D'        => '',
        'X-D'        => '',
        'XD'         => '',
        'x-D'        => '',
        'xD'         => '',
        ':\')'       => '',
        ':\'-)'      => '',
        ':-))'       => '',
        '8)'         => '',
        ':)'         => '',
        ':-)'        => '',
        ':3'         => '',
        ':D'         => '',
        ':]'         => '',
        ':^)'        => '',
        ':c)'        => '',
        ':o)'        => '',
        ':}'         => '',
        ':っ)'        => '',
        '=)'         => '',
        '=]'         => '',
        '0:)'        => '',
        '0:-)'       => '',
        '0:-3'       => '',
        '0:3'        => '',
        '0;^)'       => '',
        'O:-)'       => '',
        '3:)'        => '',
        '3:-)'       => '',
        '}:)'        => '',
        '}:-)'       => '',
        '*)'         => '',
        '*-)'        => '',
        ':-,'        => '',
        ';)'         => '',
        ';-)'        => '',
        ';-]'        => '',
        ';D'         => '',
        ';]'         => '',
        ';^)'        => '',
        ':-|'        => '',
        ':|'         => '',
        ':('         => '',
        ':-('        => '',
        ':-<'        => '',
        ':-['        => '',
        ':-c'        => '',
        ':<'         => '',
        ':['         => '',
        ':c'         => '',
        ':{'         => '',
        ':っC'        => '',
        '%)'         => '',
        '%-)'        => '',
        ':-P'        => '',
        ':-b'        => '',
        ':-p'        => '',
        ':-Þ'        => '',
        ':-þ'        => '',
        ':P'         => '',
        ':b'         => '',
        ':p'         => '',
        ':Þ'         => '',
        ':þ'         => '',
        ';('         => '',
        '=p'         => '',
        'X-P'        => '',
        'XP'         => '',
        'd:'         => '',
        'x-p'        => '',
        'xp'         => '',
        ':-||'       => '',
        ':@'         => '',
        ':-.'        => '',
        ':-/'        => '',
        ':/'         => '',
        ':L'         => '',
        ':S'         => '',
        ':\\'        => '',
        '=/'         => '',
        '=L'         => '',
        '=\\'        => '',
        ':\'('       => '',
        ':\'-('      => '',
        '^5'         => '',
        '^<_<'       => '',
        'o/\\o'      => '',
        '|-O'        => '',
        '|;-)'       => '',
        ':###..'     => '',
        ':-###..'    => '',
        'D-\':'      => '',
        'D8'         => '',
        'D:'         => '',
        'D:<'        => '',
        'D;'         => '',
        'D='         => '',
        'DX'         => '',
        'v.v'        => '',
        '8-0'        => '',
        ':-O'        => '',
        ':-o'        => '',
        ':O'         => '',
        ':o'         => '',
        'O-O'        => '',
        'O_O'        => '',
        'O_o'        => '',
        'o-o'        => '',
        'o_O'        => '',
        'o_o'        => '',
        ':$'         => '',
        '#-)'        => '',
        ':#'         => '',
        ':&'         => '',
        ':-#'        => '',
        ':-&'        => '',
        ':-X'        => '',
        ':X'         => '',
        ':-J'        => '',
        ':*'         => '',
        ':^*'        => '',
        'ಠ_ಠ'        => '',
        '*\\0/*'     => '',
        '\\o/'       => '',
        ':>'         => '',
        '>.<'        => '',
        '>:('        => '',
        '>:)'        => '',
        '>:-)'       => '',
        '>:/'        => '',
        '>:O'        => '',
        '>:P'        => '',
        '>:['        => '',
        '>:\\'       => '',
        '>;)'        => '',
        '>_>^'       => '',
        ];

    if(isset($emojis[$str])) {
        return $emojis[$str];
    }

    return null;
}

echo stringToEmoji(':)');

输出:


此答案的清单:https://stackoverflow.com/a/29581503/1580044

答案 1 :(得分:0)

我无法在此处插入整个函数,因为它绕过了堆栈溢出字符限制。这是它的链接:

https://gist.github.com/BarryMode/432a7a1f9621e824c8a3a23084a50f60#file-htmlemoji-php

很快,该功能就是

preg_replace_callback(pattern, callback, string);

字符串是输入的地方,您具有要更改为html实体的表情符号。 模式使用正则表达式在字符串中查找表情符号,然后将每个表情输入到回调中,这是从表情符号到html实体的转换。