過濾HTML以及CSS樣式等標籤__HTML

來源:互聯網
上載者:User

現在在頁面上要取出文章內容的一部分作為描述,比如下圖所示:

紅線框裡的資料是該篇文章的內容的一部分,但是文章內容儲存的時候會有html代碼等的格式

為了顯示正常需要過濾html標籤,代碼如下:

public String delHTMLTag(String htmlStr){         String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定義style的Regex         String regEx_html="<[^>]+>"; //定義HTML標籤的Regex                  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);




相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.