Several processing methods of emoji expression involved in PHP development

Source: Internet
Author: User
In recent months to do more development, storage nickname is essential can this evil support emoji expression do nickname, this is a bit of egg pain

The General MySQL table is designed with the UTF8 character set. Insert the nickname field with emoji into it, and the entire field becomes 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. What's going to happen? Let me introduce several methods.

1. Using the UTF8MB4 character set

If your MySQL version >=5.5.3, you can directly upgrade the UTF8 directly to the UTF8MB4 character set

This 4-byte UTF8 encoding is perfectly compatible with the old 3-byte UTF8 character set and can store emoji emoticons directly, which is the best solution

As for the performance loss caused by the increase in bytes, I've seen some reviews that are almost negligible

2. Using Base64 encoding

If you can't use UTF8MB4 for some reason, you can also use Base64 to curve the nation

After encoding functions such as Base64_encode, the emoji can be stored directly in the UTF8 byte set of the data table, when taken out decode.

3, kill emoji expression

Emoji expression is a troublesome thing, and even if you can store it, it doesn't have to be perfectly displayed. On a platform other than iOS, such as a PC or Android. If you need to display emoji, you have to prepare a bunch of emoji images and use a third-party front-end class library. Even so, it may be because emoji pictures are not enough to show the situation in most business scenarios, emoji is not a must. We can think of it properly and save a variety of costs.

After a hard Google, finally found a reliable code:

Filter out emoji expression 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 through each character in the string and delete it if the character is 4 bytes long.

Reprinted from: pein0119

As I recently done a small project, is solved by means of method 3, convenient

This article is also published in my blog super-violet, if you like please often come to play OH

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.