Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does PHP deal with emoji expressions in characters

Shulou Source: shulou.com Published: 2022-06-02 05:12:56 10月14日 Update

This article mainly introduces "how PHP handles emoji expressions in characters". In daily operation, I believe many people have doubts about how PHP handles emoji expressions in characters. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how PHP handles emoji expressions in characters"! Next, please follow the small series to learn together!

utf-8 encoded emoji or certain special characters take up 4 bytes. Common Chinese characters encoded by utf-8 take up 3 bytes.

Three PHP built-in functions:

mb_strlen

mixed mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] ) //Returns the number of characters in string str with encoding. Multibyte characters are counted as 1.// Returns FALSE if the given encoding is invalid

mb_substr

string mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]]) //Performs a multibyte safe substr() operation based on the number of characters. Position is counted from the start of str. The position of the first character is 0. The second character is at 1.// The mb_substr() function returns the part specified in str based on the start and length arguments.

strlen

int strlen ( string $string )//Returns the length of the given string string.

Determine whether the string contains emoji

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, 'utf-8'); if (strlen($strArr[$i]) >

= 4) { return true; } } return false;}

Remove emoji from string

The function is as follows:

function removeEmojiChar($str){ $mbLen = mb_strlen($str); $strArr = []; for ($i = 0; $i

< $mbLen; $i++) { $mbSubstr = mb_substr($str, $i, 1, 'utf-8'); if (strlen($mbSubstr) >

= 4) { continue; } $strArr[] = $mbSubstr; } return implode('', $strArr);}

String storage with emoji in MySQL

MySQL uses the utf8mb4 character set.

PHP base64 encodes strings and decodes them when pulled from the database.

Remove emoji directly from strings (this method is simple and crude)

At this point, the study of "how PHP handles emoji expressions in characters" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Characters emoticons strings processing positions functions bytes coding learning methods more utf-8 help practical special secure rough next three middle finger Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB Shulou Information Docker Linux