Home Backend Development PHP Tutorial php emoji expression processing

php emoji expression processing

Jul 29, 2016 am 09:02 AM
emoji str

背景

移动设备经常会发生用户发送的内容中包含emoji表情,未经处理,在显示时就是乱码。

解决方案

1.数据库支持:将Mysql的编码从utf8转换成utf8mb4。 

2.匹配出内容中的emoji表情做过滤或替换

/**
 * 表情转换  更新到ios9.2涵盖编码范围
 * @param $str
 * @return mixed
 */
public static function emoji_to_html($str) {
    $regex = '/([0-9|#][\x{20E3}])|[\x{00ae}|\x{00a9}|\x{203C}|\x{2047}|\x{2048}|\x{2049}|\x{3030}|\x{303D}|\x{2139}|\x{2122}|\x{3297}|\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?|[\x{1F900}-\x{1F9FF}][\x{FE00}-\x{FEFF}]?/u';
    $str = preg_replace_callback($regex,function($matches){
        $str = json_encode($matches[0]);
        $str = '<em data-emoji=&#39; . str_replace(&#39;\u&#39;, &#39;em:&#39;, $str) . &#39;></em>';
        return $str;
    },$str);
    return $str;
}
从数据库取出后再转回
$string = preg_replace_callback('/<em data-emoji=\"(.*?)\"><\/em>/is', "self::preg_emoji", $string);
/**
 * 输出emoji表情
 * @param $matches
 * @return mixed
 */
public static function preg_emoji($matches)
{
    $str = $matches[0];
    $str = str_replace('em:', '\u', $str);
    return $str;
}
Copy after login
3.直接过滤掉
/**
 * 过滤表情
 * @param $str
 * @return mixed
 */
public static function filter_emoji($str) {
    $regex = '/(\\\u[ed][0-9a-f]{3})/i';
    $str = json_encode($str);
    $str = preg_replace($regex, '', $str);
    return json_decode($str);
}
Copy after login

以上就介绍了php emoji表情处理,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Apple releases iOS 17.4 Beta 1, introducing 118 new emojis, including phoenix, lime, etc. Apple releases iOS 17.4 Beta 1, introducing 118 new emojis, including phoenix, lime, etc. Jan 26, 2024 am 08:24 AM

According to reports on January 26, according to foreign technology media emojipedia, in the iOS 17.4 Beta 1 update released today, in addition to major improvements such as sideloading and third-party app stores for testing in 27 EU countries, Apple has also added several new Emoji. The iOS 17.4 Beta 1 update adds phoenix, lime, smiling faces shaking their heads up and down, and a series of character emoticons that point the way, from the Emoji 15.1 update proposed by Unicode in September 2023. The complete Emoji are attached as follows: This update adds a total of 118 Emojis, including 6 new Emojis and 4 gender-neutral family Emojis. In addition, there are 6 existing character expressions

How to turn off emoji in Win10 Education Edition How to turn off emoji in Win10 Education Edition Feb 24, 2024 pm 01:55 PM

Emoji emoticons are the latest input method function added to Win10 Education Edition. Many cute emoticons make chatting less boring. However, some users will pop up this interface when using shortcut keys. Today I will show you how to turn off emoji in Win10 Education Edition. introduce. How to turn off emoji1 in Win10 Education Edition. First, you need to right-click the input method on the right side of the taskbar below. 2. Select Settings in the pop-up option box to enter the language setting interface. 3. Select the "Keys" option in the interface and scroll to the bottom to find "Open Emoticons and Symbols Panel", where you can turn off emoji expressions. 4. If other input methods are installed, you can enter the "Settings and Language" interface from Windows settings, and then select

How to input Huawei emoji How to input Huawei emoji Sep 26, 2023 pm 01:31 PM

Huawei emoji input method: 1. Huawei mobile phones come with an emoji keyboard. You can switch to this keyboard to enter emoji when entering text; 2. Where you need to enter emoji, long press the input box, and then select "Enter" "Method Settings", in the input method settings, you can find and select the emoji input method that comes with the system; 3. You can download and use a third-party emoji keyboard, and then browse and select the emoji you want to use on the keyboard. .

iOS 15.4 and iPadOS 15.4 Beta 1 public beta released, Face ID can be used even if you wear a mask iOS 15.4 and iPadOS 15.4 Beta 1 public beta released, Face ID can be used even if you wear a mask Apr 13, 2023 pm 11:01 PM

Apple has officially launched iOS 15.4 and iPadOS 15.4 Beta 1 to the public, version 19E5209h. The official update notes list many changes, including the addition of new features. Support for masks Face ID Apple has added “Mask Face ID” in iOS 15.4. The iPhone will authenticate users by identifying the unique characteristics of the area around the eyes when wearing a mask. Note that users must use iPhone 12 or above to use this feature. Adding 112 new Emoji expressions iOS 15.4 Beta 1 officially supports Emoji 14. Add to it

What to do if mysql emoji is garbled What to do if mysql emoji is garbled Feb 16, 2023 am 10:01 AM

Solution to the garbled mysql emoji code: 1. Check the mysql table encoding through the "show create table test ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" command; 2. Set the reading encoding to "utf8mb4" through "set names utf8mb4;"

What are the similarities and differences between __str__ and __repr__ in Python? What are the similarities and differences between __str__ and __repr__ in Python? Apr 29, 2023 pm 07:58 PM

What are the similarities and differences between __str__ and __repr__? We all know the representation of strings. Python's built-in function repr() can express objects in the form of strings to facilitate our identification. This is the "string representation". repr() obtains the string representation of an object through the special method __repr__. If __repr__ is not implemented, when we print an instance of a vector to the console, the resulting string may be. >>>classExample:pass>>>print(str(Example()))>>>

Python built-in type str source code analysis Python built-in type str source code analysis May 09, 2023 pm 02:16 PM

1The basic unit of Unicode computer storage is the byte, which is composed of 8 bits. Since English only consists of 26 letters plus a number of symbols, English characters can be stored directly in bytes. But other languages ​​(such as Chinese, Japanese, Korean, etc.) have to use multiple bytes for encoding due to the large number of characters. With the spread of computer technology, non-Latin character encoding technology continues to develop, but there are still two major limitations: no multi-language support: the encoding scheme of one language cannot be used in another language and there is no unified standard: for example There are many encoding standards in Chinese such as GBK, GB2312, GB18030, etc. Since the encoding methods are not unified, developers need to convert back and forth between different encodings, and many errors will inevitably occur.

iOS 15.4 Beta 5 introduces new features: Face ID, AirTag anti-tracking, etc. iOS 15.4 Beta 5 introduces new features: Face ID, AirTag anti-tracking, etc. Apr 21, 2023 am 08:10 AM

Apple pushes iOS15.4Beta5 and iPadOS15.4Beta5 to developers. It is estimated that this may be the final Beta. It is likely to enter the RC stage next week, and the official version will be released before mid-March. AirTag anti-tracking function The new version of iOS 15.4 introduces the AirTag and FindMy anti-tracking functions previously announced by Apple. Now they have been introduced in Beta 4, adding warning messages and adjusting the "unknown accessories detected" warning during pairing design. Siri’s new voice option For American users, iOS15.4Beta4 has added the fifth Siri voice (file name: Quinn).

See all articles