zoukankan      html  css  js  c++  java
  • 邮件链接替换

    // 将href外站的链接换为302跳转
        public static String formatA(String src, String patternStr, String replace) {
            if (StringUtil.isBlank(src) || StringUtil.isBlank(patternStr) || StringUtil.isBlank(replace)) {
                return src;
            }
            
            // html的标签属性肯定是以单引号或双引号包含
            Pattern p = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(src);
            while (m.find()) {
                String a = m.group();
                // 本站内部链接不处理
                /*if (a.contains("javaniu") || a.contains("zuidaima") || a.contains("javascript:void") || a.matches("href=['|"]?\/")) {
                    continue;
                }*/
                String _url = m.group(2);
                String url = _url;
    
                try {
                    url = URLEncoder.encode(_url, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                url = String.format(replace, url);
                // 将target属性替换为"",以便所有外链都是_blank打开
                String targetReg = "target=['|"].*?['|"']";
                String prefix = m.group(1).replaceAll(targetReg, "");
                String suffix = m.group(3).replaceAll(targetReg, "");
                // 整段匹配不会出现错误
                String _a = "<a" + prefix + " href='" + url + "'" + suffix + " target='_blank'>";
                src = src.replace(a, _a);
            }
            
            return src;
        }
    
    
        // 将添加的链接去除
        public static String formatABackUp(String src, String patternStr) {
            if (StringUtil.isBlank(src) || StringUtil.isBlank(patternStr)) {
                return src;
            }
    
            // html的标签属性肯定是以单引号或双引号包含
            Pattern p = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(src);
            while (m.find()) {
                String a = m.group();
                // 本站内部链接不处理
                /*if (a.contains("javaniu") || a.contains("zuidaima") || a.contains("javascript:void") || a.matches("href=['|"]?\/")) {
                    continue;
                }*/
                String _url = m.group(2);
                String url = _url;
    
                try {
                    url = URLDecoder.decode(_url, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                //https://tj422637.onloon.co/callback/mail/trace/hit?messageID=mid:4191b2fd09274c4ba4e6811c26dc95c4&extMessageID=extmid:21147d3a3cb14687afb9b48e28f6f879&redirect_url=https%3A%2F%2Fwww.baidu.com%2F
    //            url = String.format(replace, url);
                url = url.substring(url.indexOf("redirect_url=") + 13);
                // 将target属性替换为"",以便所有外链都是_blank打开
                String targetReg = "target=['|"].*?['|"']";
                String prefix = m.group(1).replaceAll(targetReg, "");
                String suffix = m.group(3).replaceAll(targetReg, "");
                // 整段匹配不会出现错误
                String _a = "<a" + prefix + " href='" + url + "'" + suffix + ">";
                src = src.replace(a, _a);
            }
    
            return src;
        }
    public static void main(String[] args) {
            String replace = "http://trace" + "?messageID=" + "123";
            if (StringUtil.isNotBlank("456")) {
                replace += "&extMessageID=" + "456";
            }
            replace += "&redirect_url=%s";
    
            String src = "<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#fff" style="text-align: left"><tbody><tr><td valign="top" style="100%;"><p style="font-size:18px;line-height:30px;text-align:justify;">本周五务必上线,<a href="http://www.baidu.com">如有问题</a>,及时通知!</p></td></tr><tr height="20"></tr></tbody></table>";
            //添加链接
            src = formatA(src, A_HREF_URL, replace);
            System.out.println(src);
            //去除链接
            src = formatABackUp(src, A_HREF_URL);
            System.out.println(src);
        }
    <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#fff" style="text-align: left"><tbody><tr><td valign="top" style="100%;"><p style="font-size:18px;line-height:30px;text-align:justify;">本周五务必上线,<a href='http://trace?messageID=123&extMessageID=456&redirect_url=http%3A%2F%2Fwww.baidu.com' target='_blank'>如有问题</a>,及时通知!</p></td></tr><tr height="20"></tr></tbody></table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#fff" style="text-align: left"><tbody><tr><td valign="top" style="100%;"><p style="font-size:18px;line-height:30px;text-align:justify;">本周五务必上线,<a href='http://www.baidu.com'  target='_blank'>如有问题</a>,及时通知!</p></td></tr><tr height="20"></tr></tbody></table>
  • 相关阅读:
    [Project Euler] Problem 58
    [Project Euler] Problem 59 Decrption
    [Project Euler] Problem 57
    VS2010 + WinDDK 搭建驱动开发环境
    利用C++模板特性计算各整数类型的最大最小值
    虚表的那些事儿
    ModuleNotFoundError: No module named 'pip._vendor.six'
    OpenCVPython系列之单应性查找对象理论篇
    OpenCVPython系列之背景分离
    OpenCVPython系列之Shi—tomasi拐角检测器
  • 原文地址:https://www.cnblogs.com/wanhua-wu/p/9597375.html
Copyright © 2011-2022 走看看