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

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

藏色散人
藏色散人forward
2020-07-08 13:21:224752browse

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。
  • 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 中指定的部分。
  • strlen
int strlen ( string $string )

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

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;
}

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);
}

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete