Several processing methods involving emoji expressions in PHP development

Generally, Mysql tables are designed with the UTF8 character set. Insert the nickname field with emoji into it and it will be gone, and the entire field will become an empty string. What's going on here?
It turns out that Mysql's utf8 character set is 3 bytes, and emoji is 4 bytes, so the entire nickname cannot be stored. How to do this? Let me introduce several methods
1. Use utf8mb4 character set
If your mysql version is >=5.5.3, you can directly upgrade utf8 to utf8mb4 character set.
This 4-byte utf8 encoding is perfectly compatible with the old 3-character It is the best solution to save the utf8 character set and directly store emoji expressions.
As for the performance loss caused by the increase in bytes, I have seen some evaluations, which are almost negligible.
2. Use base64 encoding
if you If you can't use utf8mb4 for some reason, you can also
use the country. The emoji encoded by functions such as base64_encode can be directly stored in the data table of the utf8 byte set, and you can decode it when you take it out.
3. Kill the emoji expression
Emoji is a cumbersome thing, and even if you can store it, it won't necessarily display perfectly. On platforms other than iOS, such as PC or android. If you need to display emoji, you have to prepare a lot of emoji images and use a third-party front-end library. Even so, there may be cases where the emoji cannot be displayed because the images are not complete enough.
In most business scenarios, emojis are not absolutely necessary. We can properly consider killing it to save various costs
//Filter out emoji expressions
function filterEmoji($str)
{
    $str = preg_replace_callback(
            '/./u',
            function (array $match) {
                return strlen($match[0]) >= 4 ? '' : $match[0];
            },
            $str);


     return $str;
 }

The basic idea is to iterate over each character in the string, and if the character is 4 bytes long, delete it.

This article is reproduced from: https://segmentfault.com/a/1190000005751031


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324612759&siteId=291194637