java如何将html过滤为纯文本
来源:互联网 发布:全景天窗 知乎 编辑:程序博客网 时间:2024/06/05 10:46
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方法的例子
简单的记录下来,表达可能不清晰希望大家不要介意!
- java如何将html过滤为纯文本
- 如何将HTML转换为纯文本格式Text
- JAVA 过滤标签将html内容转换为文本
- JAVA 过滤标签将html内容转换为文本
- Java过滤HTML标签,获取纯文本
- js将html转换为纯文本
- java过滤html标签获取纯文本信息
- 使用正则表达式将Html转换为纯文本
- 使用正则表达式将Html转换为纯文本
- 使用正则表达式将Html转换为纯文本
- Python 正则表达式将纯文本转化为HTML格式
- Java如何将Xml转换成纯文本 FreeMaker
- Java过滤任意(script,html,style)标签符,返回纯文本--封装类
- Java过滤任意(script,html,style)标签符,返回纯文本--封装类
- C# 将 HTML 转成纯文本
- PHP html 转换为纯文本
- html标签转化为纯文本格式
- 将HTMLString转换为纯文本
- 解决windows2012server中80端口被占用
- Protobuffer和json深度对比
- redis 数据结构
- 在mac下配置tp5,出现文件读写权限的问题解决办法
- 返回结果的HTTP状态码
- java如何将html过滤为纯文本
- android集成第三方微信登录
- Java中的文件上传
- 专一、自动化代码生成探讨
- 如何快速搭建一个微服务架构?
- 技术分享的好处
- 指针和数组
- reboot流程分析
- 用户管理(二):使用shell脚本批量删除除root用户以外的其它用户