Background
There is a need to use emoticons, as implemented in Tg:
- Grouping emoticons by type
- A collection of emoticons for displaying a list
- Correct storage of emoticons in the database
- Emoji search (search by tags, name, etc.)
When searching the Internet, I realized that there is no such library in PHP that would solve the problems I needed and decided to write my own library.
Emoji PHP - Link to GitHub: https://github.com/deniskorbakov/emoji-php
What is the problem
- Lack of a ready-made tool for this task
- Where to get actual emoticons from
- Storing Unicode emoticons in the database
- Search for emoticons in different languages
I managed to solve all these problems, not in the best way, but I'm quite satisfied with it.
Therefore, if you have something to add, I am waiting for your comments under the post.
Next, let's look at how it was possible to solve everything.
Problem solving
1) Lack of a ready-made tool for this task
To do this, I had to write my own library, below I will show you what it can offer you in terms of functionality.
Simple implementation
In order to install the package, you need PHP (version 8.4 or higher) and composer.
composer require deniskorbakov/emoji-php
After installation, you can work with the class for emoticons.
use DenisKorbakov\EmojiPhp\Emojis;
new Emojis();
Support for 25 languages
To use the support for different languages, I added 25 languages to the project, which provided the repository from the second point, in order to get multilingual grouping and search by emoticons.
A list with a grouping
In order to get a list of emoticons grouped by type, you need to call the following method:
use DenisKorbakov\EmojiPhp\Emojis;
use DenisKorbakov\EmojiPhp\Locale;
new Emojis()->list(Locale::EN);
// return ['smileys & emotion' => ['๐' => ':grinning_face:', ...]]
Depending on the specified Locale, you will get a grouping by the selected language.
The group will contain Unicode (smiley face) and a Short Code, which we will insert into the text or we can use a smiley face in the text, and using the method below we will replace it when saving data to the database.
Replacement of Emoticons and Short Code in the text
In order to replace Unicode in the text with Short Code and vice versa, we use the following methods:
Here we are replacing Unicode with Short Code.
use DenisKorbakov\EmojiPhp\Emojis;
$text = 'Hello, world! ๐๏ธ'
new Emojis()->toCode($text);
// return 'Hello, world! :globe_showing_europe_africa:'
Here we are replacing the Short Code with Unicode
use DenisKorbakov\EmojiPhp\Emojis;
$text = 'Hello :waving_hand:';
new Emojis()->toEmoji($text);
// return 'Hello ๐'
Keyword search
To search by keywords, we use the following method:
use DenisKorbakov\EmojiPhp\Emojis;
use DenisKorbakov\EmojiPhp\Locale;
$searchText = 'shoe'
new Emojis()->search(Locale::EN, $searchText);
// return ['๐' => ':mans_shoe:', ...]
In this method, we specify the language we want to use to perform the search and the text we are looking for.
The result is obtained by the usual coincidence of words in the tags of the searched word.
In the future, I want to improve the search, as it is not perfect, but it is quite suitable for searching by matching keywords.
2) Where to get actual emoticons from
The source of the current emoticons, I chose this repository
It already has:
- Support for more than 25 languages
- CLDR Short Codes
- Collection of relevant emoticons
3) Storing Unicode emoticons in the database
Emoticons can be stored in the database by changing the encoding to utf8mb4
I read somewhere that Discord does not store the Unicode of the smiley face and a certain Short Code in the database, and then converts it to Unicode.
After studying the topic, I realized that it is possible to use standardized Short Codes according to the CLDR specification.
CLDR (Common Locale Data Repository) is an extensive repository of local data that is used by software around the world to correctly display and work with emojis, ensuring their consistency and correct meaning in different language environments
In the second paragraph, I specified a repository that already contains the CLDR Short Code for emoticons.
With this solution, we were able to solve the problem of saving emoticons in the database without changing the encoding of the database itself.
4) Search for emoticons in different languages
For my library, I wanted to implement a keyword search in both Russian and English.
Also, because I chose to make this solution an open library, it was also worth thinking about other languages.
The second point specifies a repository that supports more than 25 languages, and therefore this process took a little time.
Unfortunately, at this stage I had to upload all 25 languages to the project, which increases the package size, in the future I want to solve this problem by adding a command to add and remove languages.
Conclusion
I told you about the problem I faced, showed you solutions to various nuances, and showed you the functionality of the library I created for this purpose.
I will also be glad to see suggestions for future features or bug reports in the issue.
GitHub - I will be glad to receive your subscription to me
Thank you for reading this article.
Top comments (0)