background
When you are doing WeChat development, you will find that storing WeChat nicknames is essential.
But this evil WeChat supports emoji expression as a nickname, which is a bit painful
Generally, when designing Mysql tables, UTF8 character sets are used. Put the nickname field with emoji insideinsert
It disappeared in an instant, and the entire field became an empty string. What's going on?
It turns out that it is because Mysql's utf8 character set is 3 bytes, while emoji is 4 bytes, so the entire nickname cannot be stored. What should I do? I'll introduce several methods
Solution
1. Use utf8mb4 character set
If your mysql version>=5.5.3
, you can directlyutf8
Upgrade directly toutf8mb4
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 byte increase, I have read some reviews, which are almost negligible.
2. Use base64 encoding
If you can't use utf8mb4 for some reason, you can also usebase64
Come and save the country in a curve
Use for examplebase64_encode
The encoded emoji of functions like this can be directly stored in the data table of the utf8 byte set, and then decode it when extracted.
3. Kill the emoji expression
Emoji expressions are a troublesome thing, and even if you can store them, they may not be able to display them 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, it is still possible that emoji cannot be displayed because the emoji pictures are not complete enough. In most business scenarios, emoji is not necessary. We can consider killing it appropriately and save all kinds of costs
After a lot of hard work on Google, I finally found a reliable and useful code:
// Filter out emoji expressionsfunction filterEmoji($str) { $str = preg_replace_callback( '/./u', function (array $match) { return strlen($match[0]) >= 4 ? '' : $match[0]; }, $str); return $str; }
The above are several processing methods for emoji expressions in PHP WeChat development summarized for you. The basic idea is to traverse each character in the string. If the length of the character is 4 bytes, delete it. Hope everyone likes it!