0

I want a regular expression which can strip all comments from the HTML using PHP.

I saw some threads on stackoverflow like Regexp match strickly html comment string, but the regex provided there doesn't work. My PHP code outputs nothing after I apply the provided code.

I have written:

$regex = array('/<!--((.*)!(\[if))-->/Uis', "/[[:blank:]]+/");
$replaced_comment_in_html = preg_replace($regex, '', $html);

But it shows comments the HTML:

<!-- This is my test comment, which I want to be removed in HTML  -->
<!--[if lt IE 9]>
    <script src="something.js"></script>
<![endif]-->

It does not remove the comments that I want to be removed, and if I write the below regex, then it removes all comments (also the IE style and scripts, which are required on the page)

$regex = array('/<!--(.*)-->/Uis', "/[[:blank:]]+/");

Can someone help?

1

1 Answer 1

1

Use this regex:

<!--[^\[].*-->

This will not remove IE comments, but will remove other comments.

Use it like this:

$regex = array('/<!--[^\[].*-->/Uis', "/[[:blank:]]+/");
$replaced_comment_in_html = preg_replace($regex, '', $html);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.