HtmlUtils.java
3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Copyright 2015-2025 FLY的狐狸(email:jflyfox@sina.com qq:369191470).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.jflyfox.util.extend;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* html处理
*
* 2015年2月2日 下午3:39:59 flyfox 330627517@qq.com
*/
public class HtmlUtils {
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标签的正则表达式
/**
* 去除Html标签
*
* 2015年2月2日 下午3:40:49 flyfox 330627517@qq.com
*
* @param htmlStr
* @return
*/
public static String delHTMLTag(String htmlStr) {
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(); // 返回文本字符串
}
/**
* 去除Script标签
*
* 2017年1月18日 下午3:11:31 flyfox 330627517@qq.com
*
* @param htmlStr
* @return
*/
public static String delScriptTag(String htmlStr) {
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标签
return htmlStr.trim(); // 返回文本字符串
}
/**
* 修改标签特殊字符
*
* @请使用changeSpecialCode
*
* 2015年5月18日 下午2:00:35 flyfox 330627517@qq.com
* @param content
* @return
*/
public static String changeTag(String content) {
return changeSpecialCode(content);
}
/**
* 修改标签特殊字符
*
* 2015年5月18日 下午2:00:35 flyfox 330627517@qq.com
*
* @param content
* @return
*/
public static String changeSpecialCode(String content) {
content = content.replaceAll("&", "&");
content = content.replaceAll("<", "<");
content = content.replaceAll(">", ">");
content = content.replaceAll("\"", """);
content = content.replaceAll("'", "'");
content = content.replaceAll("/", "/");
return content;
}
/**
* 删除标签特殊字符
*
* 2015年6月9日 下午9:30:16 flyfox 330627517@qq.com
*
* @param content
* @return
*/
public static String delSpecialCode(String content) {
content = content.replaceAll("&", "");
content = content.replaceAll("<", "");
content = content.replaceAll(">", "");
content = content.replaceAll("\"", "");
content = content.replaceAll("'", "");
content = content.replaceAll("/", "");
return content;
}
}