How to explode unicode Emojis in php

1.1k Views Asked by At

I am using cometchat for website. And now I am developing react api, and for that they are not providing any supports so I need to create emoji api for IOS and Android Chat application. In DB we are storing emojis as IMG tag. We have prepared an array like below

$symbols = array(
    ':n100:' => '\ud83d\udcaf',
    ':n1234:' => '\ud83d\udd22',
    ':a:' => '\ud83c\udd70',
    ':ab:' => '\ud83c\udd8e',
    ':abc:' => '\ud83d\udd24',
    ':abcd:' => '\ud83d\udd21',
    ':accept:' => '\ud83c\ude51',
    ':aquarius:' => '\u2652',
    ':aries:' => '\u2648',
);
$smiley = array(
':n100:': 'n100.png',
':n1234:':'n1234.png', ....
);

Now the main issue is in the string return by IOS and Android app. I could not parse smiley from that string. Below is the string examples.

Str1: "Lorem Ipsum \u23eb \u23ec"
Str2: "lorem \u23ec\u23eb Ipsum"
Str3: "\ud83d\udd04\ud83d\udd3c"

As you can see that in string1,string2 there are two smiles, 1 with space separator and another without space separator. And in string3, it also contains only two smiles ("\ud83d\udd04","\ud83d\udd3c").

Can you guys please help me in parsing the smiley or Unicode string and explode the message as well as smiles from string?

I want to parse these smiles to compare it with above mentioned arrays $symbols and based on the key, I need to display images from that array only.

Thanks in advance.

1

There are 1 best solutions below

2
On BEST ANSWER

JSON's \u can only handle one UTF-16 code unit at a time, so you need to write the surrogate pair instead. For U+1F600 this is \uD83D\uDE00, which works:

echo json_decode('"\uD83D\uDE00"');