Procházet zdrojové kódy

html标签去除

Signed-off-by: anderx <312518615@qq.com>
anderx před 5 roky
rodič
revize
88017b802d

+ 38 - 0
src/main/java/com/kede/common/controller/WebpageController.java

@@ -3,6 +3,8 @@ package com.kede.common.controller;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
@@ -191,8 +193,44 @@ public class WebpageController extends BaseController {
 
 
 	public String displayRestriction(String str) {
+		//去除HTML标签
+		str=removeHtmlTag(str);
 		if(str.length()>24)str=str.substring(0, 24)+"...";
 		return str;
 	}
 	
+	
+	
+	/**
+	* 删除Html标签
+	*/
+	public static String removeHtmlTag(String htmlStr) {
+	//定义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]*?>";
+	//定义HTML标签的正则表达式
+	String regEx_html = "<[^>]+>";
+	//定义一些特殊字符的正则表达式 如:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+	String regEx_special = "\\&[a-zA-Z]{1,10};";
+
+//	//1.过滤script标签
+//	Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
+//	Matcher m_script = p_script.matcher(htmlStr);
+//	htmlStr = m_script.replaceAll("");
+//	//2.过滤style标签
+//	Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
+//	Matcher m_style = p_style.matcher(htmlStr);
+//	htmlStr = m_style.replaceAll("");
+	//3.过滤html标签
+	Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
+	Matcher m_html = p_html.matcher(htmlStr);
+	htmlStr = m_html.replaceAll("");
+	//4.过滤特殊标签
+	Pattern p_special = Pattern.compile(regEx_special, Pattern.CASE_INSENSITIVE);
+	Matcher m_special = p_special.matcher(htmlStr);
+	htmlStr = m_special.replaceAll("");
+
+	return htmlStr;
+	}
 }