• java如何将html过滤为纯文本


      java开发中jsp页面可以嵌套很多插件就可以将html形式的文本直接转化为纯文本,但是如果你已经保存下来或者没有运用插件,这个额html形式的文本你该怎么转化为纯文本呢?有次我将公告保存了html形式的,展示是直接将这个html放在对应的区域内就可以,然而,在写接口是,另外一方需要纯文本的,而我页面没有运用这个插件,怎么做?最终得以解决,希望对需要的人有用。

      html文本:

    <p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微软雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);">
        引用<a href="http://www.divcss5.com/" style="color: rgb(0, 0, 204);">CSS</a>文件到<a href="http://www.divcss5.com/html/" style="color: rgb(0, 0, 204);">Html</a>方法-<strong>css引入</strong>,<strong>css引用</strong>
    </p>
    <p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微软雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);">
        使用不同的方法来引用<a href="http://www.divcss5.com/rumen/r29.shtml" style="color: rgb(0, 0, 204);">css样式</a>表,最终到达的效果相同,但是使用不同方法应用的<a href="http://www.divcss5.com/rumen/r72.shtml" style="color: rgb(0, 0, 204);">css文件</a>将影响到SEO及网页打开速度效率。
    </p>
    <p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微软雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);">
        html引用css方法如下<br/>1、直接在<a href="http://www.divcss5.com/" style="color: rgb(0, 0, 204);">div</a>中使用css样式制作<a href="http://www.divcss5.com/" style="color: rgb(0, 0, 204);">div+css</a>网页<br/>2、html中使用style自带式<br/>3、使用@import引用外部CSS文件<br/>4、使用<a href="http://www.divcss5.com/html/h64.shtml" style="color: rgb(0, 0, 204);">link</a>引用外部CSS文件 推荐此方法
    </p>
    <p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微软雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);">
        <span style="color: rgb(255, 0, 0);">扩展阅读</span>:<a href="http://www.divcss5.com/rumen/r431.shtml" style="color: rgb(0, 0, 204);">link与import区别</a>
    </p>
    <p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微软雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);">
        接下来我们将逐个讲解html引用css方法的例子
    </p>
    <p>
        <br/>
    </p>
    

        

      我们现在运用正则表达式对这个html文本进行处理,使用正则表达式可以最快速的过滤到html标签,这个方法遗留的问题就是有时候空格不清楚是用户敲的还是其他原因放上去的,在本方法中我将所有空格过去掉了,如果你不需要可以去掉那句代码,代码如下:

          //将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(); //返回文本字符串
        }

       最后的字符串就是如下图所示这样:

    引用CSS文件到Html方法-css引入,css引用使用不同的方法来引用css样式表,最终到达的效果相同,但是使用不同方法应用的css文件将影响到SEO及网页打开速度效率。html引用css方法如下1、直接在div中使用css样式制作div+css网页2、html中使用style自带式3、使用@import引用外部CSS文件4、使用link引用外部CSS文件推荐此方法扩展阅读:link与import区别接下来我们将逐个讲解html引用css方法的例子
    

      

        简单的记录下来,表达可能不清晰希望大家不要介意!

        

  • 相关阅读:
    Android中WebView如何加载JavaScript脚本
    Android中WebView如何加载JavaScript脚本
    Android中WebView如何加载JavaScript脚本
    Android如何使用SQLlite数据库
    Android如何使用SQLlite数据库
    Android如何使用SQLlite数据库
    __declspec(dllimport)的作用
    __declspec,__cdecl,__stdcall都是什么意思?有什么作用?
    #pragma pack(push,1)与#pragma pack(1)的区别
    #pragma pack(n) 的作用
  • 原文地址:https://www.cnblogs.com/angusbao/p/7489583.html
Copyright © 2020-2023  润新知