Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does PHP deal with emoji expressions in characters

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report