数据库导出文章,如何过滤其中的HTML标签

堕落的唐僧 2014-12-04 12:17:53
如题,文章存进数据库是带着<BR/>这样的HTML标签,现在在页面中用EL表达式fn:substring截断输出时,如何过滤掉这些标签,防止<这种符号出现?
求详细代码,急。。
...全文
311 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zy_think123 2014-12-09
  • 打赏
  • 举报
回复
这个最好在入库的时候就进行处理掉,可以使用正则表达式替换掉,我有这样一个过滤Demo,可以直接使用的

 public static String getonerow(String HTMLStr)
	{
		String htmlStr = HTMLStr;
		String textStr = "";
		java.util.regex.Pattern p_script;
		java.util.regex.Matcher m_script;
		java.util.regex.Pattern p_style;
		java.util.regex.Matcher m_style;
		java.util.regex.Pattern p_html;
		java.util.regex.Matcher m_html;
		try
		{
			String regEx_script = "<[//s]*?script[^>]*?>[//s//S]*?<[//s]*?///[//s]*?script[//s]*?>"; 
			String regEx_style = "<[//s]*?style[^>]*?>[//s//S]*?<[//s]*?///[//s]*?style[//s]*?>"; 
			String regEx_html = "<[^>]+>";
			p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
			m_script = p_script.matcher(htmlStr);
			htmlStr = m_script.replaceAll(""); 
			p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
			m_style = p_style.matcher(htmlStr);
			htmlStr = m_style.replaceAll(""); 
			p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
			m_html = p_html.matcher(htmlStr);
			htmlStr = m_html.replaceAll("");
			textStr = htmlStr.replaceAll(" "," ");
			textStr = htmlStr.replaceAll("<",  "<");
			textStr = htmlStr.replaceAll(">",  ">");
			textStr = htmlStr.replaceAll("®", "®");
			textStr = htmlStr.replaceAll("&", "&");
		}
		catch (Exception e)
		{
			System.err.println("Html2Text: " + e.getMessage());
		}
		return textStr;
	}
-阿克蒙德- 2014-12-08
  • 打赏
  • 举报
回复
引用 8 楼 u013794332 的回复:
[quote=引用 7 楼 u012047741 的回复:] 过滤喽!要么在数据库查询的时候,利用sql的函数进行过滤;要么就在java程序里面进行过滤
有成功的实例吗,网上的JAVA代码好像都不起作用啊[/quote] 这种东西自己写就好了,哪这么多实例。sql的话去百度下replace函数之类的,java的话replaceall之类的 自己动动脑筋,动动手指查查,别什么东西都伸手要好吧,又不是多复杂的东西。
堕落的唐僧 2014-12-08
  • 打赏
  • 举报
回复
引用 7 楼 u012047741 的回复:
过滤喽!要么在数据库查询的时候,利用sql的函数进行过滤;要么就在java程序里面进行过滤
有成功的实例吗,网上的JAVA代码好像都不起作用啊
-阿克蒙德- 2014-12-08
  • 打赏
  • 举报
回复
过滤喽!要么在数据库查询的时候,利用sql的函数进行过滤;要么就在java程序里面进行过滤
堕落的唐僧 2014-12-08
  • 打赏
  • 举报
回复
好像都没用哎
堕落的唐僧 2014-12-04
  • 打赏
  • 举报
回复
引用 1 楼 ghx287524027 的回复:
方案一: 在数据库中不存入HTML标签的有关信息,过滤HTML标签,只显示文字。具体实现方式参考以下链接: http://my.oschina.net/parker/blog/49229 方案二: public String delHTMLTag(String htmlStr){ String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 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标签 htmlStr=htmlStr.replace(" ",""); htmlStr=htmlStr.replaceAll("\\s*|\t|\r|\n",""); htmlStr=htmlStr.replace("“",""); htmlStr=htmlStr.replace("”",""); htmlStr=htmlStr.replaceAll(" ",""); return htmlStr.trim(); //返回文本字符串 程序中只需调用String content = delHTMLTag(content); 即可。
方案2 程序中报The method delHTMLTag(String) is undefined for the type错误啊
ghx287524027 2014-12-04
  • 打赏
  • 举报
回复
方案一: 在数据库中不存入HTML标签的有关信息,过滤HTML标签,只显示文字。具体实现方式参考以下链接: http://my.oschina.net/parker/blog/49229 方案二: public String delHTMLTag(String htmlStr){ String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 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标签 htmlStr=htmlStr.replace(" ",""); htmlStr=htmlStr.replaceAll("\\s*|\t|\r|\n",""); htmlStr=htmlStr.replace("“",""); htmlStr=htmlStr.replace("”",""); htmlStr=htmlStr.replaceAll(" ",""); return htmlStr.trim(); //返回文本字符串 程序中只需调用String content = delHTMLTag(content); 即可。
tony4geek 2014-12-04
  • 打赏
  • 举报
回复
tony4geek 2014-12-04
  • 打赏
  • 举报
回复
引用 2 楼 u013794332 的回复:
[quote=引用 1 楼 ghx287524027 的回复:] 方案一: 在数据库中不存入HTML标签的有关信息,过滤HTML标签,只显示文字。具体实现方式参考以下链接: http://my.oschina.net/parker/blog/49229 方案二: public String delHTMLTag(String htmlStr){ String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 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标签 htmlStr=htmlStr.replace(" ",""); htmlStr=htmlStr.replaceAll("\\s*|\t|\r|\n",""); htmlStr=htmlStr.replace("“",""); htmlStr=htmlStr.replace("”",""); htmlStr=htmlStr.replaceAll(" ",""); return htmlStr.trim(); //返回文本字符串 程序中只需调用String content = delHTMLTag(content); 即可。
方案2 程序中报The method delHTMLTag(String) is undefined for the type错误啊[/quote] 应该是内容里面有其他标签。
堕落的唐僧 2014-12-04
  • 打赏
  • 举报
回复
大家有成功的实例吗

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧