zoukankan      html  css  js  c++  java
  • java之自动过滤提交文本中的html代码script代码

    public class test {
        public static String Html2Text(String inputString) {
            String htmlStr = inputString; // 含html标签的字符串
            String textStr = "";
            Pattern p_script;
            Matcher m_script;
            Pattern p_style;
            Matcher m_style;
            Pattern p_html;
            Matcher m_html;
            Pattern p_html1;
            Matcher m_html1;
            try {
                String regEx_script = "<[//s]*?script[^>]*?>[//s//S]*?<[//s]*?///[//s]*?script[//s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[//s//S]*?<///script>
                String regEx_style = "<[//s]*?style[^>]*?>[//s//S]*?<[//s]*?///[//s]*?style[//s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[//s//S]*?<///style>
                String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
                String regEx_html1 = "<[^>]+";
                p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
                m_script = p_script.matcher(htmlStr);
                htmlStr = m_script.replaceAll(""); // 过滤script标签
    
                p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
                m_style = p_style.matcher(htmlStr);
                htmlStr = m_style.replaceAll(""); // 过滤style标签
    
                p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
                m_html = p_html.matcher(htmlStr);
                htmlStr = m_html.replaceAll(""); // 过滤html标签
    
                p_html1 = Pattern.compile(regEx_html1, Pattern.CASE_INSENSITIVE);
                m_html1 = p_html1.matcher(htmlStr);
                htmlStr = m_html1.replaceAll(""); // 过滤html标签
                textStr = htmlStr;
            } catch (Exception e) {
            
            }
            return textStr;// 返回文本字符串
        }
    public static void main(String[] args) {
        String url = Html2Text("<a Style='color:red'>你是傻逼</a>");
        System.out.println(url);
        
    }
    }
  • 相关阅读:
    Django视图之ORM数据库查询操作API
    Django视图之ORM更改数据库连接——配置MySQL库
    TLPI读书笔记第2章-基本概念2
    TLPI读书笔记第2章-基本概念1
    linux防火墙常用命令
    linux配置yum软件仓库
    linux进程管理
    linux文件权限
    linux用yum安装软件
    linux磁盘划分
  • 原文地址:https://www.cnblogs.com/tohxyblog/p/6605744.html
Copyright © 2011-2022 走看看