Use MySQL UTF8 to store more than three bytes of emoji expression without upgrading MySQL

Source: Internet
Author: User
Tags stringbuffer

Since the version of the database is now 5.5.2 , but to look at the Internet to directly store emoji emoticons, you need to upgrade to 5.5.3 and then set the CharSet utf8mb4 , but the upgrade database feels sensitive operation.

Consider how long after the direct consideration of using regular to replace, but emoji the expression of the code unicode too much, in the Internet to find a code, or 2 years ago updated, think or forget.

PHP version

The simplest way is to directly find the received text in the string 4 bytes of content, do rawurlencode code, and then storage. The output of the time to replace the back, and then use the rawurldecode transcoding back unicode to the phone and then resolve to the emoji expression.

Gets the length mb_strlen of the string, by character, mb_substr to intercept the string (which mb_strcut is different by byte).

Store
‘‘; $length = mb_strlen($str,‘utf-8‘); for ($i=0; $i < $length; $i++) {    $_tmpStr = mb_substr($str,$i,1,‘utf-8‘); if(strlen($_tmpStr) >= 4){ $strEncode .= ‘[[EMOJI:‘.rawurlencode($_tmpStr).‘]]‘; }else{ $strEncode .= $_tmpStr; }} echo $strEncode."\n";// 周梦康123~[[EMOJI:%F0%9F%98%81]][[EMOJI:%F0%9F%98%84]]
Read
//转码回去$strDecode = preg_replace_callback("/\[\[EMOJI:(.*?)\]\]/", function($matches){ return rawurldecode($matches[1]);}, $strEncode); echo $strDecode."\n";

PHP is so easily and happily solved.

Java version

Considering that you want to use the routines in PHP, the original string to get its length, if more than 3 bytes, then the code can be stored, according to the previous experience, I wrote the following code

"\uD83D\uDE01";// 一个 emoji 表情byte[] bytes = a.getBytes("utf-8");System.out.println(bytes.length);// 输出 4

A single individual to get to the correct number of bytes, but how to ensure that the time to cut the string is correct? I searched all the streets of the city, none, and found my way.

Import Java.net.URLEncoder;Import Java.net.URLDecoder;Import Java.util.regex.Matcher;Import Java.util.regex.Pattern;Import java.io.UnsupportedEncodingException;PublicClasstest{PublicStaticvoidMain(string[] args)Throws Exception {String content = args[0]; SYSTEM.OUT.PRINTLN (content);int strlength = Content.length (); String filtercontent = emojifilter (content); System.out.println (filtercontent); String emojistr = Emojirecovery (filtercontent); System.out.println (EMOJISTR); }PrivateStatic StringEmojifilter(String str) {String patternstring ="([\\x{10000}-\\x{10ffff}\ud800-\udfff])"; Pattern pattern = pattern.compile (patternstring); Matcher Matcher = Pattern.matcher (str); StringBuffer SB =New StringBuffer ();while (Matcher.find ()) {try {matcher.appendreplacement (SB),"[[EMOJI:" + Urlencoder.encode (Matcher.group (1),"UTF-8") +"]]"); }catch (Unsupportedencodingexception e) {e.printstacktrace ();}} Matcher.appendtail (SB);return sb.tostring ();} private static string emojirecovery(String str) {string patternstring = "\\[\\[emoji: (. *?)" \\]\\]"; Pattern pattern = pattern.compile (patternstring); Matcher Matcher = Pattern.matcher (str); StringBuffer sb = new StringBuffer (); While (Matcher.find ()) { try {matcher.appendreplacement (SB, Urldecoder.decode (Matcher.group (1)," UTF-8 ")); } catch (Unsupportedencodingexception e) {e.printstacktrace ();}} matcher.appendtail (SB); return sb.tostring ();}}            

Use MySQL UTF8 to store more than three bytes of emoji expression without upgrading MySQL

Related Article

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.