Cleaning or Removing HTML comments is one of the main part of Optimizing web pages. Here is the solution to Remove HTML Comments with PHP.
To Remove HTML Comments in PHP, We need to use the following Technique.
![]() |
Remove HTML Comments with PHP |
<?php // Start OutputBuffer ob_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Remove HTML Comments</title> </head> <body> <!-- // HTML Comments for code identification --> <!-- Mutiple Closing Tags--> <h1>Remove HTML Comments Example</h1> <!-- Unclose End Tag--> <p>This is an example to remove HTML Comments <p> </body> </html> <?php // Store HTML Output Buffer as variable with ob_get_clean(); $html = ob_get_clean(); // Specify configuration $config = array( 'indent' => false, 'hide-comments' => true, 'output-xhtml' => true, 'wrap' => false ); // Tidy $tidy = new tidy; $tidy->parseString($html, $config, 'utf8'); $tidy->cleanRepair(); // Output echo $tidy; ?>