Home > Common Problem > body text

PHP handles emoji expressions in characters (judgment/removal/storage)

藏色散人
Release: 2020-07-08 13:21:22
forward
4783 people have browsed it

PHP handles emoji expressions in characters (judgment/removal/storage)

Directory

  • Determine whether the string contains emoji expressions
  • Remove the emoji expressions in the string
  • Storage of strings containing emoji expressions in MySQL

utf-8 encoded emoji expressions or some special characters occupy 4 bytes. Common Chinese characters encoded in UTF-8 occupy 3 bytes.

Determine whether the string contains emoji expression

Three PHP built-in functions:

  • mb_strlen
mixed mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )


// 返回具有 encoding 编码的字符串 str 包含的字符数。 多字节的字符被计为 1。
// 如果给定的 encoding 无效则返回 FALSE。
Copy after login
  • mb_substr
string mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] )

// 根据字符数执行一个多字节安全的 substr() 操作。 位置是从 str 的开始位置进行计数。 第一个字符的位置是 0。第二个字符的位置是 1。
// mb_substr() 函数根据 start 和 length 参数返回 str 中指定的部分。
Copy after login
  • strlen
int strlen ( string $string )

// 返回给定的字符串 string 的长度。
Copy after login

The function is as follows:

function haveEmojiChar($str)
{
    $mbLen = mb_strlen($str);
    
    $strArr = [];
    for ($i = 0; $i < $mbLen; $i++) {
        $strArr[] = mb_substr($str, $i, 1, &#39;utf-8&#39;);
        if (strlen($strArr[$i]) >= 4) {
            return true;
        }
    }
    
    return false;
}
Copy after login

Remove emoji from the string The emoticon

function is as follows:

function removeEmojiChar($str)
{
    $mbLen = mb_strlen($str);
    
    $strArr = [];
    for ($i = 0; $i < $mbLen; $i++) {
        $mbSubstr = mb_substr($str, $i, 1, &#39;utf-8&#39;);
        if (strlen($mbSubstr) >= 4) {
            continue;
        }
        $strArr[] = $mbSubstr;
    }
    
    return implode(&#39;&#39;, $strArr);
}
Copy after login

Storage of strings containing emoji expressions in MySQL

1. Use utf8mb4 characters in MySQL set.

2. PHP base64 encodes the string, and then decodes the string when fetching it from the database.

3. Directly remove emoji expressions from the string (this method is simple and crude)

The above is the detailed content of PHP handles emoji expressions in characters (judgment/removal/storage). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
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