Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert emoji symbol code ':)' to smiley emoji in PHP [duplicate]

Tags:

php

I want to convert the emoji code i.e, ":)" to ๐Ÿ˜„.

How can I convert it?

Currently I am using p3k detector to detect the emoji's. Below is the code where I can detect the emoji's.

  $emoji = \Emoji\detect_emoji($text);
like image 455
Maneesh Rao Avatar asked Nov 16 '25 18:11

Maneesh Rao


2 Answers

You need to create your own function to convert (or search online if there is any library that already do this).

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(':)');

Output:

๐Ÿ˜„

Got list from this answer: https://stackoverflow.com/a/29581503/1580044

like image 56
Felippe Duarte Avatar answered Nov 19 '25 10:11

Felippe Duarte


I cannot insert the whole function here because it bypasses stack overflow characters limit. Here's the link to it:

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

Shortly, the function is

preg_replace_callback(pattern, callback, string);

Where the string is the input where you have emoji that you want to change into html entities. The pattern uses regex to find the emoji in the string and then each one is fed into the callback, which is where the conversion happens from emoji to html entity.

like image 42
Kamran Poladov Avatar answered Nov 19 '25 09:11

Kamran Poladov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!