过滤html标签的代码
public String html2Text(String inputString) {
String htmlStr = inputString; //含html标签的字符串
String textStr = "" ;
Pattern p_script;
Matcher m_script;
Pattern p_style;
Matcher m_style;
Pattern p_html;
Matcher m_html;
try {
String regEx_script = " <[\s]*?script[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?script[\s]*?> " ; //定义script的正则表达式{或<script>]*?>[\s\S]*?
<\/script> }
String regEx_style = " <[\s]*?style[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?style[\s]*?> " ; //定义style的正则表达式{或<style>]*?>[\s\S]*?<\/style> } String regEx_html = " <[^>]+> " ; //定义HTML标签的正则表达式
p_script = Patternpile(regEx_script,Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = placeAll( "" ); //过滤script标签
p_style = Patternpile(regEx_style,Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = placeAll( "" ); //过滤style标签
p_html = Patternpile(regEx_html,Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = placeAll( "" ); //过滤html标签
textStr = htmlStr;
} catch (Exception e) {
replaceall()
}
return textStr; //返回⽂本字符串
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论