Clear HTML tags but keep some of the labels

Source: Internet
Author: User

Most of the time we are clearing HTML tags but always want to keep some of the tags

Implement in ASP language

Str= "<a href=" "a.htm" ">a.htm</a><div>afefe</div>feaa<b>bbb</b><a href= Http://www.baidu.com id= "" GGG ">ggg</a><abbr> test </abbr>" Set re=new RegExp    re. IgnoreCase =true    Re. Global=true    Re. Pattern= "(< (?! /? (a|p|b) (\s|>)) [^>]*>) "    str=re.replace (str," ")    

The regular can also be implemented in other languages.

The following are implemented in the PHP language:

$str = ' <a href= ' a.htm ' >a.htm</a><div>afefe</div>feaa<b>bbb</b><a href=http ://www.baidu.com id= "GGG" >ggg</a><abbr> test </abbr> ' echo preg_replace ('/(< (?! \/? (a|p|b) (\s|\>)) [^>]*>)/', ', $str);?>

Of course, PHP has a simpler way of implementing it:

$str = ' <a href= ' a.htm ' >a.htm</a><div>afefe</div>feaa<b>bbb</b><a href=http ://www.baidu.com id= "GGG" >ggg</a><abbr> test </abbr> "Echo strip_tags ($str, ' <b><a> <p> ');



Clear HTML tags but keep some of the labels

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.