PHP uses regular expressions to remove annotation methods from HTML _php instances

Source: Internet
Author: User

The most recent source file in the project that needs to output the browser needs to be stripped of the comments in HTML. Read a lot of programs on the Internet, but many of the answers are the same, and can not solve my problem, so I wrote the regular expression, but also have a more profound understanding.

The first is to compare the basics:

$a = ' <!--Ceshi-->ceshi ';
$a = preg_replace (' #<!--. *--># ', ', $a);
Var_dump ($a);

The code above will output Ceshi.

But if it's the string below, it's not going to work the way we want it to.

$a = ' <!--ceshi-->ceshi<!--ceshi--> ';
$a = preg_replace (' #<!--. *--># ', ', $a);
Var_dump ($a);

So we'll change the matching rules to the following format

 
 

But in HTML if there is <!--[if Lt IE 9]>ceshi<! [endif]--> Such code can not be removed, so we need to improve the matching rules, change to the following format

 
 

And then if there's a <script><!--ceshi//--></script> code in HTML, we need to change our matching rules to the following format

 
 

In that case, I basically removed the comments I needed to get rid of the HTML!

The above is a small series for everyone to bring PHP use regular expressions to remove the annotation method in HTML all content, I hope that we support cloud Habitat Community ~

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.