3 ways to easily handle the problem of emoji expression in PHP development _php skills

Source: Internet
Author: User
Tags base64 mysql version

Background

When you do a micro-credit development, you will find that storing a micro-letter nickname is essential.

But this evil kind of micro-letter support emoji expression to do nickname, this is a bit of a pain in the egg

In general, the MySQL table is designed with the UTF8 character set. The nickname field with emoji is gone inside insert , and the entire field becomes an empty string. What the hell is going on here?

The original is because MySQL's UTF8 character set is 3 bytes, and emoji is 4 bytes, so that the entire nickname can not be stored. What's going to happen? Let me introduce some methods.

Solution

1. Use 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 code is perfect for compatibility with the old 3-byte UTF8 character set, and can store emoji expressions directly, and is the best solution
As for the performance loss caused by the byte increase, I've seen some reviews that are almost negligible

2. Using Base64 code

If you can't use UTF8MB4 for some reason, you can also use the base64 curve to save the nation.
Emoji that are encoded with functions such as, for example base64_encode , can be stored directly in the UTF8 byte set's datasheet, decode when taken out

3, kill emoji expression

Emoji expression is a troublesome thing, even if you can store it, it is not necessarily a perfect display. On a platform other than iOS, such as PCs or Android. If you need to display emoji, you have to prepare a lot of emoji pictures and use a Third-party front-end class library. Even so, it is possible that the emoji picture is not enough to show the situation in most business scenarios, emoji is not essential. We can consider killing it properly, saving 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 above is for you to summarize the PHP micro-letter development involves emoji expression of several processing methods, the basic idea is to traverse each character in the string, if the length of the character is 4 bytes, it will be deleted. I hope you like it!

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.