Home >Backend Development >PHP Tutorial >Several processing methods involving emoji expressions in PHP development
In recent months, I have been doing a lot of WeChat development, and it is essential to store WeChat nicknames
But this damn WeChat supports emoji expressions as nicknames, which is a bit painful
General Mysql When designing tables, the UTF8 character set is used. Insert the nickname field with emoji into it and it will disappear. The entire field will become an empty string. What's going on?
It turns out that the utf8 character set of Mysql is 3 bytes, and emoji is 4 bytes, so the entire nickname cannot be stored. What to do? Let me introduce several methods
1. Use utf8mb4 character set
If your mysql version >=5.5.3, you can directly upgrade utf8 directly For the utf8mb4 character set
This 4-byte utf8 encoding is perfectly compatible with the old 3-byte utf8 character set, and can directly store emoji expressions, which is the best solution
As for The performance loss caused by the increase in bytes, I have read some reviews, is almost negligible
2. Use base64 encoding
If you have some reasons If you cannot use utf8mb4, you can also use base64 to save the country.
The emoji encoded using a function such as base64_encode can be directly stored in the data table of the utf8 byte set, and you can decode it when taking it out.
3. Get rid of emoji expressions
Emoji expressions are a troublesome thing. Even if you can store them, they may not be displayed 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 still be situations where the emoji images cannot be displayed because the emoji images are not complete enough
In most business scenarios, emojis are not necessary. We can consider getting rid of it appropriately and save various costs
After a lot of hard google, we finally found a reliable and usable code:
// 过滤掉emoji表情 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 traverse the string For each character, if the length of the character is 4 bytes, it is deleted.
The above is the detailed content of Several processing methods involving emoji expressions in PHP development. For more information, please follow other related articles on the PHP Chinese website!