java去掉富文本编辑器html标签

package com.incon.project.app_util.util.removeTag;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**

  • @方法描述 去掉html元素

*/
@SuppressWarnings(“all”)
public class RemoveHtml {

private static boolean check_notEmpty_pcs(Object obj) {
	if(obj instanceof List){
		List list = (List)obj;
		return list != null && !list.isEmpty();
	}else if(obj instanceof Set){
		Set set = (Set)obj;
		return set != null && !set.isEmpty();
	}else if(obj instanceof Map){
		Map map = (Map)obj;
		return map != null && !map.isEmpty();
	}else if(obj instanceof String){
		return obj!=null&&!"".equals(obj)&&!"null".equals(obj)&&!"undefined".equals(obj);
	}else if(obj instanceof Integer){
		return obj!=null&&!"".equals(obj)&&!"null".equals(obj)&&!"undefined".equals(obj);
	}else{
		return false;
	}
}

public static String htmlRemoveTag(String inputString) {
	if (!check_notEmpty_pcs(inputString)) {
		return null;
	}
	String htmlStr = cleanXSS(inputString); // 含html标签的字符串
	String textStr = "";
	Pattern p_script;
	java.util.regex.Matcher m_script;
	Pattern p_style;
	java.util.regex.Matcher m_style;
	Pattern p_html;
	java.util.regex.Matcher m_html;
	try {
		// 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
		String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
		// 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
		String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
		String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
		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标签
		textStr = htmlStr;
		textStr = textStr.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "");
		textStr = textStr.replaceAll("[(/>)<]", "");
	} catch (Exception e) {
		e.printStackTrace();
	}
	textStr = cleanXSS(textStr);
	return textStr;// 返回文本字符串
}

private static String cleanXSS(String value) {
	value = value.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
	value = value.replaceAll("&#40;", "\\(").replaceAll("&#41;", "\\)");
	value = value.replaceAll("&#39;", "'");
	value = value.replaceAll("&quot;", "\"");
	value = value.replaceAll("\"\"", "[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']");
	value = value.replace("\r\n", "").replace(" ", "");
	return value;
}

public static void main(String[] args) {
	String cc = "<p>\r\n" + "</p>\r\n" + "<h1 align=\"center\" style=\"margin-top:15.0pt;margin-right:0cm;margin-bottom:15.0pt;\r\n" + "margin-left:0cm;text-align:center;line-height:22.5pt\"><span style=\"font-size:\r\n" + "19.5pt;color:black\">走访慰问送温暖 贴心交流促成长</span></h1>\r\n"
			+ "<p style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; text-indent: 14pt;\"><strong style=\"\"><span style=\"font-family: \" 等线=\"\" light\";=\"\" font-size:=\"\" 14pt;=\"\" color:=\"\" rgb(81,=\"\" 81,=\"\" 81);=\"\" font-weight:=\"\" normal;\"=\"\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span></strong><strong style=\"font-size: 12pt; text-indent: 14pt;\"><span 等线=\"\" light\";=\"\" font-size:=\"\" 14pt;=\"\" color:=\"\" rgb(81,=\"\" 81,=\"\" 81);\"=\"\">——</span><span style=\"font-size: 14pt; color: rgb(81, 81, 81);\">建筑工程技术系团总支深入学生寝室进行安全检查及慰问新生</span></strong></p>\r\n"
			+ "<p style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: 等线; line-height: 1.5;\"><strong><span style=\"font-size:18.0pt;font-family:'Arial',sans-serif;\r\n" + "color:#515151;font-weight:normal\">&nbsp; &nbsp; &nbsp;&nbsp;</span></strong></p>\r\n"
			+ "<p style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: 等线; line-height: 1.5;\"><span style=\"font-size:16.0pt;font-family:宋体;color:#515151\">&nbsp; &nbsp; 新学期伊始,为</span><span style=\"font-size:16.0pt;font-family:宋体;color:black\">深</span><span style=\"font-size:16.0pt;font-family:宋体;color:#515151\">入了解新生入校住宿和生活情况,及时解决新生遇到的各种问题,帮助新生更快更好地融入大学生活,<span>2019</span>年<span>8</span>月<span>31</span>日晚<span>20</span>点到<span>22</span>点<span>30</span>分,建工系团总支书记吴士超及学生会干部到男寝进行安全检查及慰问新生。</span></p>\r\n" + "<p style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: 等线; line-height: 1.5;\"><span style=\"font-size:16.0pt;font-family:宋体;color:#515151\"><br>\r\n" + "</span></p>\r\n"
			+ "<p style=\"margin: 0cm 0cm 0.0001pt; font-size: 10.5pt; font-family: 等线; text-align: left; text-indent: 32pt; line-height: 1.5;\" align=\"left\"><span style=\"font-size:16.0pt;font-family:宋体;color:#515151\">首先,吴书记了解了寝室整体情况,询问新生的基本情况、来校后的适应情况,以及对学习和生活环境的感触等。此外,还仔细查看了宿舍的基础设施配备情况,嘱咐学生干部进一步做好设施完善工作。期间,吴书记表达了对新生的殷切希望,希望大家能够尽快转变角色,有计划地安排大学生活,提早制定职业生涯规划,充分利用大学的各种学习资源,提升整体能力和自身综合素质,尽快融入到大学生活中去。\r\n" + "</span></p>\r\n" + "<p style=\"font-size: 12pt; font-family: 等线; margin: 15pt 0cm 52.5pt; text-align: left; text-indent: 32pt; line-height: 1.5;\" align=\"left\"><span style=\"font-size:16.0pt;font-family:宋体;color:#515151\">初秋送爽,情谊正浓。老师们的走访送暖之行激励和温暖着新生们的心,相信<span>2019</span>级建工系学子会怀揣着这份温暖,以乘风破浪会有时的豪情,书写“直挂云帆济沧海”的新篇章,经营好自己的大学生活,遇见更美好的自己。</span></p>\r\n"
			+ "<p style=\"margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:12.0pt;font-family:等线;margin-top:15.0pt;margin-right:0cm;margin-bottom:52.5pt;\r\n" + "margin-left:0cm;text-align:left;line-height:18.0pt\" align=\"left\"><span style=\"font-size:16.0pt;font-family:宋体;color:#515151\">&nbsp; <img width=\"497\" height=\"414\" id=\"图片 6\" src=\"/inconmh/resources/ewebeditor/uploadfile/20190901085034684001.png\" style=\"width: 493px; height: 414px;\"><img width=\"496\" height=\"343\" id=\"图片 5\" src=\"/inconmh/resources/ewebeditor/uploadfile/20190901085034950002.png\" style=\"width: 496px; height: 415px;\"></span></p>\r\n"
			+ "<p style=\"margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:12.0pt;font-family:等线;margin-top:15.0pt;margin-right:0cm;margin-bottom:52.5pt;\r\n" + "margin-left:0cm;text-align:left;line-height:18.0pt\" align=\"left\"><span style=\"font-size:16.0pt;font-family:宋体;color:#515151\"><img width=\"498\" height=\"410\" id=\"图片 4\" src=\"/inconmh/resources/ewebeditor/uploadfile/20190901085034527003.png\"></span></p>\r\n" + "<p style=\"margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:12.0pt;font-family:等线;margin-top:15.0pt;margin-right:0cm;margin-bottom:52.5pt;\r\n"
			+ "margin-left:0cm;text-align:left;line-height:18.0pt\" align=\"left\"><span style=\"font-size:16.0pt;font-family:宋体;color:#515151\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(供稿人:纪晓薇)</span></p>\r\n" + "<p style=\"margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:12.0pt;font-family:等线;margin-top:15.0pt;margin-right:0cm;margin-bottom:52.5pt;\r\n" + "margin-left:0cm;text-align:left;line-height:18.0pt\" align=\"left\"><span style=\"font-size:16.0pt;font-family:宋体;color:#515151\"><br>\r\n" + "</span></p>\r\n"
			+ "<p style=\"margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:12.0pt;font-family:等线;text-indent:28.0pt;line-height:18.0pt\"><span style=\"font-size:16.0pt;font-family:宋体;color:#515151\">&nbsp;</span></p>\r\n" + "<p style=\"margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:12.0pt;font-family:等线;margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:等线\"><span>&nbsp;</span></p>\r\n"
			+ "<p style=\"margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:12.0pt;font-family:等线;margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:等线\"><span>&nbsp;</span></p>\r\n" + "<br>\r\n" + "<p></p>\r\n" + "";
	System.out.println(htmlRemoveTag(cc));
}

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值