Filter HTML and CSS style tags __html

Source: Internet
Author: User
Tags html tags

Now you want to take a part of the content of the article as a description on the page, as shown in the following illustration:

The data in the red box is part of the content of the article, but the content of the article will be stored in the format of HTML code, etc.

To display the normal need to filter the HTML tags, the code is as follows:

public string Delhtmltag (string htmlstr) { 
        string regex_style= ' <style[^>]*?>[\\s\\S]*?<\\/style> "; The regular expression 
        String regex_html= "<[^>]+>" that defines the style;//defines the regular expression pattern P_style=pattern.compile for HTML tags 
         
        ( regex_style,pattern.case_insensitive); 
        Matcher M_style=p_style.matcher (HTMLSTR); 
        Htmlstr=m_style.replaceall (""); Filter style label pattern 
         
        p_html=pattern.compile (regex_html,pattern.case_insensitive); 
        Matcher M_html=p_html.matcher (HTMLSTR); 
        Htmlstr=m_html.replaceall (""); Filter HTML tags 
        
        htmlstr=htmlstr.replace ("", "");
        Htmlstr=htmlstr.replaceall ("\\s*|\t|\r|\n", "");
        Htmlstr=htmlstr.replace ("" "," ");
        Htmlstr=htmlstr.replace ("" "," ");
        Htmlstr=htmlstr.replaceall ("", "");
          
        return Htmlstr.trim (); Returns a text string 
    
The program simply calls:

String content = delhtmltag (content);




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.