Solution to using emoji expressions in php projects

*文
Release: 2023-03-18 21:58:01
Original
2252 people have browsed it

In daily life, it often happens that the content sent by users contains emoji expressions. Without processing, the content will be garbled when displayed. Therefore, this article introduces several processing methods involving emoji expressions in PHP WeChat development. I hope to be helpful.

Background

When you are doing WeChat development, you will find that storing WeChat nicknames is essential.

But the evil WeChat supports emoji expressions as nicknames, which is a bit painful

Generally, when designing Mysql tables, the UTF8 character set is used. When you insert the nickname field with emoji inside, it disappears and the entire field becomes 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

Solution

1. Use utf8mb4 character set

If your mysql version>=5.5.3, you can directly upgrade utf8 to utf8mb4 character set
This 4-byte utf8 encoding is perfectly compatible The old 3-byte utf8 character set, which can directly store emoji expressions, is the best solution.
As for the performance loss caused by the increase in bytes, I have read some reviews and it is almost negligible

2. Use base64 encoding

If you cannot use utf8mb4 for some reason, you can also use base64 to save the country
Use For example, the emoji encoded by functions such as base64_encode can be directly stored in the data table of the utf8 byte set, and can be decoded when taken 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, it may still be impossible to display because emoji images are not complete enough. In most business scenarios, emoji is not necessary. We can consider getting rid of it appropriately and save various costs

After a lot of hard google, I 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;
 }
Copy after login

Related recommendations:

How to generate mysql data dictionary in php

php implements word statistics function

How PHP handles binary data

The above is the detailed content of Solution to using emoji expressions in php projects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!