public class EmojiUtil {
private static final String EMOJI = "[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]";
//特殊的字符,识别不到的
private static final String[] SPECIAL_EMOJIS = {
"☮", "☯", "☪", "☦", "✴", "✳", "☠", "☢", "⁉", "☣", "4⃣", "☹", "☸", "✨", "☺", "✡", "✝", "☎", "✔", "☄",
"✖", "☂", "3⃣", "☃", "☀", "✒", "☁", "✌", "✍", "☝", "✏", "✈", "✉", "✊", "✋", "☘", "✅", "#⃣", "☕", "☔",
"✂", "☑", "♨", "♠", "♣", "♥", "♦", "〰", "♻", "♿", "6⃣", "❣", "〽", "❤", "⌨", "♊", "♋", "♈", "♉", "♎",
"♏", "♌", "♍", "❓", "ℹ", "❕", "❔", "❗", "⌛", "⌚", "™", "❌", "❎", "5⃣", "♓", "♒", "♑", "♐", "❄", "‼",
"❇", "⏺", "⏸", "⏹", "⚡", "⚠", "➰", "⏲", "➿", "⏳", "⏰", "⏱", "⚫", "⚪", "⏫", "⏪", "⏩", "⚰", "⏯", "⚱",
"⏮", "➡", "⏭", "⏬", "0⃣", "⚽", "⚾", "8⃣", "➕", "➖", "©", "➗", "▶", "®", "⚖", "⭐", "⚗", "⚔", "⭕", "⚒",
"⚓", "▫", "▪", "7⃣", "⚜", "⚛", "⚙", "↘", "↙", "Ⓜ", "⛩", "⛪", "↔", "↕", "↖", "↗", "⛰", "⛱", "⛲", "⛳",
"⛴", "⛵", "◀", "㊙", "⛷", "㊗", "⛸", "⛹", "⛺", "⛽", "⬅", "⬆", "⬇", "⛅", "⛄", "◾", "◼", "⛈", "◽",
"⛏", "⛎", "◻", "*⃣", "1⃣", "⛓", "↪", "↩", "⛑", "⤵", "⛔", "⤴", "⬜", "9⃣", "⬛", "2⃣"};
private EmojiUtil() {
}
private static String encryptEmojiByDES(String strWithEmoji) {
String rt;
if (!Str.isEmpty(strWithEmoji)) {
StringBuilder ret = new StringBuilder();
String destemp = encryptEmojiByDESTemp(strWithEmoji);
try {
for (String emoji : SPECIAL_EMOJIS) {
if (destemp.contains(emoji)) {
destemp = ret.append(destemp.replace(emoji, "</</*/" + DESUtil.encryptDES(emoji) + "/*/>/>")).toString();
break;
}
}
boolean is = false;
for (String emoji : SPECIAL_EMOJIS) {
if (is = destemp.contains(emoji)) {
break;
}
}
if (is) {
destemp = encryptEmojiByDES(destemp);
}
rt = destemp;
} catch (Exception e) {
e.printStackTrace();
rt = strWithEmoji;
}
} else {
rt = strWithEmoji;
}
return rt;
}
/***
* 过滤表情特殊字符
*/
public static String filterEmoji(String strWithEmoji) {
String rt;
if (!Str.isEmpty(strWithEmoji)) {
StringBuilder ret = new StringBuilder();
String destemp = filterEmojiTemp(strWithEmoji);
for (String emoji : SPECIAL_EMOJIS) {
if (destemp.contains(emoji)) {
destemp = ret.append(destemp.replace(emoji, "")).toString();
break;
}
}
boolean is = false;
for (String emoji : SPECIAL_EMOJIS) {
if (is = destemp.contains(emoji)) {
break;
}
}
if (is) {
destemp = filterEmoji(destemp);
}
rt = destemp;
} else {
rt = strWithEmoji;
}
P.p("过滤表情后 值 = " + rt);
return rt;
}
private static String decryptEmojiByDES(String str) {
String ret = str;
if (!Str.isEmpty(str)) {
while (ret.contains("</</*/") && ret.contains("/*/>/>")) {
try {
int begin = ret.indexOf("</</*/");
int end = ret.indexOf("/*/>/>");
String sub = ret.substring(begin, end + 6);
String rmStart = sub.replace("</</*/", "");
String rmEnd = rmStart.replace("/*/>/>", "");
ret = ret.replace(sub, DESUtil.decryptDES(rmEnd));
} catch (Exception e) {
e.printStackTrace();
return str;
}
}
}
return ret;
}
private static String encryptEmojiByDESTemp(String strWithEmoji) {
StringBuffer sb = new StringBuffer();
if (!Str.isEmpty(strWithEmoji)) {
Pattern pattern = Pattern.compile(EMOJI);
Matcher matcher = pattern.matcher(strWithEmoji);
matcher.reset();
while (matcher.find()) {
try {
String matcherStr = matcher.group();
String en = DESUtil.encryptDES(matcherStr);
matcher.appendReplacement(sb, "</</*/" + en + "/*/>/>");
} catch (Exception e) {
e.printStackTrace();
return strWithEmoji;
}
}
matcher.appendTail(sb);
}
String retStr = sb.toString();
return Str.isEmpty(retStr) ? strWithEmoji : retStr;
}
private static String filterEmojiTemp(String strWithEmoji) {
if (StringUtils.isNotBlank(strWithEmoji)) {
return strWithEmoji.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "");
} else {
return strWithEmoji;
}
}
}
过滤字符串表情符
最新推荐文章于 2024-08-12 00:09:05 发布