去除字符串中的HTML标签
背景:Kindeditor内容保存在数据库中的类型是text,包含文字和HTML标签。
需求:显示内容的前50个字(纯文字内容)
方法:将字段查出去除标签,截取前50
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class StrUtils { private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
private static final String regEx_img = "<img\\s*([^>]*)\\s*src=\\\"(.*?)\\\"\\s*([^>]*)>";// 定义image标签的正则表达式
private static final String regEx_emoji = "[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\ud83e\\udd00-\\ud83e\\udfff]|[\\u2600-\\u27ff]";// 定义表情标签的正则表达式
private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
private static final String regEx_special = "\\&[a-zA-Z]{1,10};";//定义特殊字符 public static String delHTMLTag(String htmlStr) { // 过滤script标签
Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
Matcher m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤style标签
Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
Matcher m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 过滤image标签
Pattern p_img = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
Matcher m_img = p_img.matcher(htmlStr);
htmlStr = m_img.replaceAll(""); // 过滤emoji标签
Pattern p_emoji = Pattern.compile(regEx_emoji, Pattern.CASE_INSENSITIVE);
Matcher m_emoji = p_emoji.matcher(htmlStr);
htmlStr = m_emoji.replaceAll(""); // 过滤html标签
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
Matcher m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤空格回车标签
Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
Matcher m_space = p_space.matcher(htmlStr);
htmlStr = m_space.replaceAll(""); // 过滤特殊字符
Pattern p_special = Pattern.compile(regEx_special, Pattern.CASE_INSENSITIVE);
Matcher m_special = p_special.matcher(htmlStr);
htmlStr = m_special.replaceAll(""); return htmlStr.trim(); // 返回文本字符串
} public static String getTextFromHtml(String htmlStr){
htmlStr = delHTMLTag(htmlStr);
htmlStr = htmlStr.replaceAll(" ", "");
if (htmlStr.length()>50){
htmlStr = htmlStr.substring(0,50);
}
return htmlStr;
} }
去除字符串中的HTML标签的更多相关文章
- js去除字符串中所有html标签及 符号
近日在做项目的时候,经常会在页面上处理一些数据.结果发现自己js掌握的并不是很好.那就在这里记录js的点点滴滴吧. 1. 去除字符串中的 html 标签 function delHtmlTag(str ...
- 正则去除字符串中的html标签,但不去除<br>标签
一.去除html标签 filterHTMLTag(msg) { var msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag msg ...
- php去除字符串中的HTML标签
php自带的函数可以去除/删除字符串中的HTML标签/代码. strip_tags(string,allow):函数剥去 HTML.XML 以及 PHP 的标签. 参数:string,必填,规定要检查 ...
- (ASP.NET )去除字符串中的HTML标签
string strDoContent = "执行增加<a href="/AdminCX/Admin_CompanyDetail.aspx?CompanyGuid=cd8e1 ...
- js去除字符串中的标签
var str="<p>js去除字符串中的标签</p>"; var result=str.replace(/<.*?>/ig,"&qu ...
- java 去html标签,去除字符串中的空格,回车,换行符,制表符
public static String getonerow(String allLine,String myfind) { Pattern ...
- 正则匹配去掉字符串中的html标签
1.得到超链接中的链接地址: string matchString = @"<a[^>]+href=\s*(?:'(?<href>[^']+)'|"&quo ...
- 147-PHP strip_tags函数,剥去字符串中的 HTML 标签(一)
<?php $html=<<<HTM <title>PHP输出HTML代码</title> <body> <a href=#>转 ...
- 去除字符串中的html标记及标记中的内容
去除字符串中的html标记及标记中的内容 --1.创建函数 create function [dbo].[clearhtml] (@maco varchar(8000)) returns varcha ...
随机推荐
- POJ 1797 Heavy Transportation (Dijkstra)
题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...
- JasperReports教程
JasperReports教程 Jasper报表是一个开源的Java报表引擎,是基于Java的,它没有自己的表达式语法. 由于JasperReports是一个Java类库,而不是针对最终用户,而是有针 ...
- 元类,sqlalchemy查询
import sqlalchemy from sqlalchemy.ext.declarative import declarative_base #创建连接实例 db = sqlalchemy.cr ...
- Python37不能启动pyspider
报错内容: Traceback (most recent call last): File "/usr/local/var/pyenv/versions/3.7.3/bin/pyspider ...
- [已解决]报错: twisted 18.7.0 requires PyHamcrest>=1.9.0
1.下载对应的Twisted,下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted 2.通过Anaconda3的Anaconda Promp ...
- [BJWC2018]上学路线
Description 小B 所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M). 小B 家住在西南角,学校在东北角.现在有T 个路口进行施工,小B 不能通过这些路口.小B ...
- goland 实用键
代码补全 option + command + v
- wait/notify方法
执行wait方法会释放锁,执行notify不会释放锁 package com.qf.test05.pojo; /** * @author qf * @create 2018-09-18 10:41 * ...
- 第一章 笔记本电脑安装Linux系统(Centos7)
目标:通过[Linux+Docke+Nginx+Jenkins+k8s(Kubernetes)+CICD(自动化)]进行项目部署 内容:根据个人进度实时分章节记录自己所遇到的问题 一.准备工作 1.下 ...
- NFS(网络文件系统)
NFS(网络文件系统) 1.关于NFS介绍 1.1NFS在企业中的应用场景 在企业集群架构的工作场景中,NFS网络文件系统一般被用来存储共享视频,图片,附件等静态资源文件,通常网站用户上传的文件都会放 ...