MYSQL write emoji emoji character processing

Source: Internet
Author: User

This ghost emoji expression is 4 bytes, MySQL uses UTF8 encoding,UTF8 accounts for 3 bytes, to store that emoji expression needs to change the MySQL encoding from UFT8 to UFT8 superset,UTF 8MB4;

It is easy to change the database code to cause large-face garbled disaster. So when encountering emoji character expression, do special treatment. Online also have a lot of processing solutions, finally found a sticker address and code: Https://github.com/BriquzStudio/php-emoji, Thank you

Class emoji{/** * Encode Emoji in text * @param string $text text to Encode */public static function Encode ($text) {return Self::convertemoji ($text, "ENCODE");} /** * Decode emoji in text * @param string $text text to Decode */public static function Decode ($text) {return self::conve Rtemoji ($text, "DECODE");} private static function Convertemoji ($text, $op) {if ($op = = "ENCODE") {return Preg_replace_callback ('/([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{2600}-\X{27BF}] [\x{1f000}-\x{1feff}]?| [\x{2900}-\x{297f}] [\x{fe00}-\x{feff}]?| [\X{2B00}-\X{2BF0}] [\x{fe00}-\x{feff}]?| [\X{1F000}-\X{1F9FF}] [\x{fe00}-\x{feff}]?| [\X{1F000}-\X{1F9FF}] [\x{1f000}-\x{1feff}]?/u ', Array (' self ', ' Encodeemoji '), $text);}else{return Preg_replace_callback ('/(\\\u[0-9a-f]{4}) +/', Array (' self ', ' Decodeemoji '), $text);}} private static function Encodeemoji ($match) {return str_replace (Array (' [', '] ', ' "'), '", Json_encode ($match));} private static function Decodeemoji ($text) {if (! $text) return '; $text = $text [0]; $decode = Json_decode ($text, true); if ($ decode) return $decode; $text = ' ['. $text. '] '; $decode = Json_decode ($text); if (count ($decode) = = 1) {return $decode [0];} return $text;}}

 

$nickName = Emoji::D ecode ($userinfo [' nickname ']);

$realName = Empty ($nickName)? ' User: '. Time (): $nickName;

MYSQL write emoji emoji character processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.