PHP is involved in the development of emoji expression of several processing methods!

Source: Internet
Author: User

Emoji expression processing

The General MySQL table is designed with the UTF8 character set. The nickname field with emoji is gone insert , 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 utf8 upgrade 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 the base64 curve to salvation
Using functions such as base64_encode the emoji can be stored directly in the UTF8 byte set of the data table, when taken out decode a bit

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 the emoji picture is not enough to appear in the situation can not be displayed
In most business scenarios, emoji is not a must-have. 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

PHP is involved in the development of emoji expression of several processing methods!

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.