Java 过滤 html、script、style 代码得到纯字符串 方法

  1. /*** 
  2.  
  3. * @param content 内容String 
  4. * @param p >0 .位数 
  5. * @return @tale: 
  6. * @purpose:得到相应位数已过滤html、script、style 标签的内容 内容结尾 为... 
  7. * @author:Simon - 赵振明 
  8. * @CreationTime:Aug 25, 201011:07:06 AM 
  9. */  
  10. public static String getNoHTMLString(String content,int p){  
  11.     
  12.     if(null==content) return "";  
  13.     if(0==p) return "";  
  14.     
  15.     java.util.regex.Pattern p_script;   
  16.          java.util.regex.Matcher m_script;   
  17.          java.util.regex.Pattern p_style;   
  18.          java.util.regex.Matcher m_style;   
  19.          java.util.regex.Pattern p_html;   
  20.          java.util.regex.Matcher m_html;   
  21.           
  22.      try {   
  23.          String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";  
  24.          //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> }    
  25.          String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";   
  26.                //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }    
  27.                String regEx_html = "<[^>]+>"//定义HTML标签的正则表达式    
  28.              
  29.                p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);   
  30.                m_script = p_script.matcher(content);   
  31.                content = m_script.replaceAll(""); //过滤script标签   
  32.                p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);   
  33.                m_style = p_style.matcher(content);   
  34.                content = m_style.replaceAll(""); //过滤style标签    
  35.              
  36.                p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);   
  37.                m_html = p_html.matcher(content);   
  38.                  
  39.                content = m_html.replaceAll(""); //过滤html标签    
  40.            }catch(Exception e) {   
  41.                    return "";  
  42.            }   
  43.     
  44.            if(content.length()>p){  
  45.             content = content.substring(0, p)+"...";  
  46.            }else{  
  47.             content = content + "...";  
  48.            }  
  49.     
  50.     
  51.     
  52.    return content;  
  53. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值