java 过滤HTML标签

Java如何过滤掉HTML标签呢?

java过滤html标记   java删除html标签

Java代码   收藏代码
  1. /** 
  2.      * 删除input字符串中的html格式 
  3.      *  
  4.      * @param input 
  5.      * @param length 
  6.      *            显示的字符的个数 
  7.      * @return 
  8.      */  
  9.     public static String splitAndFilterString(String input, int length) {  
  10.         if (input == null || input.trim().equals("")) {  
  11.             return "";  
  12.         }  
  13.         // 去掉所有html元素,  
  14.         String str = input.replaceAll("\\&[a-zA-Z]{1,10};""").replaceAll(  
  15.                 "<[^>]*>""");  
  16.         str = str.replaceAll("[(/>)<]""").trim();  
  17.         str=SystemHWUtil.deleteAllCRLF(str);//delete all CRLF  
  18.         int len = str.length();  
  19.         if (length==SystemHWUtil.NEGATIVE_ONE|| len <= length) {  
  20.             return str;  
  21.         } else {  
  22.             str = str.substring(0, length);  
  23.             str += "......";  
  24.         }  
  25.         return str;  
  26.     }  
  27.   
  28.     /** 
  29.      * 返回纯文本,去掉html的所有标签,并且去掉空行 
  30.      *  
  31.      * @param input 
  32.      * @return 
  33.      */  
  34.     public static String splitAndFilterString(String input) {  
  35.         if (input == null || input.trim().equals("")) {  
  36.             return "";  
  37.         }  
  38.         // 去掉所有html元素,  
  39.         String str = input.replaceAll("\\&[a-zA-Z]{1,10};""").replaceAll(  
  40.                 "<[^>]*>""");  
  41.         str = str.replaceAll("[(/>)<]""");  
  42.         return SystemHWUtil.deleteCRLF(str);  
  43.     }  

 应用:

Java代码   收藏代码
  1. paperNews.setSplitAndFilterString(SystemHWUtil.splitAndFilterString(paperNews.getContent(), 120/*TODO */));  
  2.   
  3. ring content=paperNews.getContent();  
  4.     paperNews.setContent(SystemHWUtil.splitAndFilterString(content, DictionaryParam.getInt("news_settings""content_max")));  
  5.       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值