时间:2021-07-01 10:21:17 帮助过:24人阅读
//转码回去
$strDecode = preg_replace_callback("/\[\[EMOJI:(.*?)\]\]/", function($matches){
return rawurldecode($matches[1]);
}, $strEncode);
echo $strDecode."\n";
php 就这么轻松愉快的解决了。
考虑到想使用php里面的套路,对原字符串逐个字符获取其长度,如果超过3个字节,那么则编码下,就能存储了,依照上次的经验,我写了如下代码
String a = "\uD83D\uDE01";// 一个 emoji 表情
byte[] bytes = a.getBytes("utf-8");
System.out.println(bytes.length);// 输出 4
单个单个的来,能获取到正确的字节数,但是如何保证切割字符串的时候是正确的呢?我找遍了城市所有的街道,都没有,找到我要的方法.
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.UnsupportedEncodingException;
public class test{
public static void main(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);
}
private static String emojiFilter(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();
}
}
在不升级 mysql 的情况下直接使用 mysql utf8 存储 超过三个字节的 emoji 表情
标签:for cal fun recovery str 使用 经验 imp log