过滤字符串表情符


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;
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值