问题
无法保存emoji表情到mysql(字符集为utf-8)?
原因
emoji表情也是utf-8编码,但是占用4个字节,而mysql的utf-8字符集的数据库每个字符只有3个字节,所以无法保存emoji表情到mysql数据库。
解决方法
一、修改数据库字符集为utf8mb4
在5.5.3版本之后的mysql数据库支持utf8mb4字符集,可以保存4个字节的emoji表情。需要修改数据库、表、字段的字符集为utf8mb4。
这是最简单有效的方法。
二、将emoji表情转换成可以保存的格式
我试过的有两种方式,一种是以urlencode转换,一种是以base64加密,都是可以将emoji表情保存为mysql数据库(utf-8)的方法,但也有很明显的缺陷:
(1)每次输入和输出都要进行加密和解密,会影响效率,虽然影响不大
(2)一个emoji表情,正常情况下只用一个字符就能保存,但urlencode处理后需要12字节(%03%25%17%37的格式),base64需要6字节(8J-Sjg的格式),除此之外还需正则识别的标志,比如[[8J-Sjg]],其中[[]]是为了正则表达式定位用的。另注意:若处理json格式的字符串,最好不要用[[]],可能保存数据库中的结果变成[["8J-Sjg"]]。
emoji表情的正则表达式
发现一个很不错的正则,实验后善未发现无法识别的表情(ios ipad上实验)
input.replaceAll("[\\x{10000}-\\x{10ffff}\ud800-\udfff]", "");
来源:http://stackoverflow.com/questions/27820971/why-a-surrogate-java-regexp-finds-hypen-minus
样例
/**
* @Description 将字符串中的emoji表情转换成可以在utf-8字符集数据库中保存的格式(表情占4个字节,需要utf8mb4字符集)
* @param str
* 待转换字符串
* @return 转换后字符串
* @throws UnsupportedEncodingException
* exception
*/
public static String emojiConvert1(String str)
throws UnsupportedEncodingException {
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,
"[["
+ URLEncoder.encode(matcher.group(1),
"UTF-8") + "]]");
} catch(UnsupportedEncodingException e) {
LOG.error("emojiConvert error", e);
throw e;
}
}
matcher.appendTail(sb);
LOG.debug("emojiConvert " + str + " to " + sb.toString()
+ ", len:" + sb.length());
return sb.toString();
}
/**
* @Description 还原utf8数据库中保存的含转换后emoji表情的字符串
* @param str
* 转换后的字符串
* @return 转换前的字符串
* @throws UnsupportedEncodingException
* exception
*/
public static String emojiRecovery2(String str)
throws UnsupportedEncodingException {
String patternString = "\\[\\[(.*?)\\]\\]";
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) {
LOG.error("emojiRecovery error", e);
throw e;
}
}
matcher.appendTail(sb);
LOG.debug("emojiRecovery " + str + " to " + sb.toString());
return sb.toString();
}