zoukankan      html  css  js  c++  java
  • 处理html内容,获取纯文本

    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    /**
     * @Author: lzw
     * @Description: 过滤html标签,转化为纯文本
     * @Date: Created in 13:56 2018-03-20
     */
    public class DelHtmlTagUtil {
    
        //将html转换为纯文本,此方法最后保留了&nbps空格,使用时注意将空格替换掉
        public static String delHTMLTag(String htmlStr) {
            String regEx_script = "<script[^>]*?>[\s\S]*?<\/script>"; //定义script的正则表达式
            String regEx_style = "<style[^>]*?>[\s\S]*?<\/style>"; //定义style的正则表达式
            String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式
    
            Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
            Matcher m_script = p_script.matcher(htmlStr);
            htmlStr = m_script.replaceAll(""); //过滤script标签
    
            Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
            Matcher m_style = p_style.matcher(htmlStr);
            htmlStr = m_style.replaceAll(""); //过滤style标签
    
            Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            Matcher m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll(""); //过滤html标签
    
            return htmlStr.trim(); //返回文本字符串
        }
    
    }
    

      

  • 相关阅读:
    Tire树的理解和应用
    C语言:socket简单模拟http请求
    C语言:关于socket的基础知识点
    php中的ip2long和long2ip的理解
    理解php中的pack/unpack/ord/chr
    zlog学习笔记(mdc)
    计算机工作的进行
    期末总结
    第十四周学习报告
    第十三周学习报告
  • 原文地址:https://www.cnblogs.com/wiseroll/p/9360184.html
Copyright © 2011-2022 走看看