Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove comments from html source code

Tags:

php

curl

I know how to get the html source code via cUrl, but I want to remove the comments on the html document (I mean what is between <!-- .. -->). In addition, if I can take just the BODY of the html document. thank you.

like image 351
Luis Avatar asked Jun 10 '11 11:06

Luis


People also ask

How do I hide comments in HTML?

To create hidden comments in HTML, add <! --- tag and end it with -- >. Whatever comes inside it is hidden. These comments are beneficial for those who need to refer the code again, which allow them to easily understand the code.

How do you delete a comment in CSS?

CSS comments are eliminated with the rewrite_css filter, and Javascript comments are eliminated with the rewrite_javascript filter. The filter reduces the transfer size of HTML files by removing most HTML comments.

How do you comment in HTML code?

An HTML comment begins with <! –– and the comment closes with ––> . HTML comments are visible to anyone that views the page source code, but are not rendered when the HTML document is rendered by a browser.

How do you delete all comments in VS code?

Once installed, remove comments in your code by opening the command palette ( Ctrl + Shift + P ), entering "Remove all comments" and pressing enter. Voilà, all the comments are gone 🎉 !


1 Answers

Regex solved this problem for me as follows:

function remove_html_comments($html = '') {
    return preg_replace('/<!--(.|\s)*?-->/', '', $html);
}
like image 80
andromeda Avatar answered Oct 12 '22 14:10

andromeda