How to minify HTML code of PHP page ?

HTML output minification is important to improve website performance by reducing the page load times and reduce the overall page size. Minifying HTML output also reduces the data usage of the user requesting the site.
Minification can be done by removing unnecessary details and eliminating excessive whitespaces, newlines, comments, etc. However, minification reduces the readability of the code. Minification can reduce file size upto 70%. PHP is used to transfer files from development to production environment. HTML files can be minified both manually and automatically. Minification can be undone using several tools that add whitespaces in the code. However, any comments removed during minification cannot be restored.

Example: This is the HTML file without minifying the the code.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
  
<!-- This is the content that 
    shows in the browser tab -->
      
<title>Title Page</title>
  
</head>
  
<body>
  
<!-- This is a comment. -->
  
<h1>Hello world!</h1>
</body>
  
</html>

chevron_right


HTML file after minification

<html><head><title>Title Page</title></head><body><h1>Hello world!</h1></body></html>

Approach 1: Using GZip Compression in Apache:



Steps to enable Gzip Compression in Apache

  1. Open the Apache configuration file
    vim /etc/httpd/conf/httpd.conf 
  2. Check the following line in the configuration file.
    LoadModule deflate_module modules/mod_deflate.so
  3. Add the following lines at the end of configuration file.
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    
  4. Restart the Apache Server
    sudo service httpd restart

Approach 2: HTML code can be minified with ob_start() function with a callback.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
ob_start("minifier");
function minifier($code) {
    $search = array(
          
        // Remove whitespaces after tags
        '/\>[^\S ]+/s',
          
        // Remove whitespaces before tags
        '/[^\S ]+\</s',
          
        // Remove multiple whitespace sequences
        '/(\s)+/s',
          
        // Removes comments
        '/<!--(.|\s)*?-->/'
    );
    $replace = array('>', '<', '\\1');
    $code = preg_replace($search, $replace, $code);
    return $code;
}
?>
<!DOCTYPE html>
<html>
   
<head>
   
<!-- title of page -->
   
<title>Demo for minifier</title>
   
</head>
   
<body>
   
<!-- body of page -->
   
<h1>Hello World</h1>
   
</body>
   
</html>
   
<?php
ob_end_flush();
?>

chevron_right


Output:

<!DOCTYPE html><html><head><title>Demo for minifier</title></head><body><h1>Hello World</h1></body></html>

Approach 3: Using HTMLMinifier plugin: HTML Minifier is a server-side source code minifier designed to optimise HTML, CSS and JavaScript output sent out to the client by removing unnecessary whitespaces, comments and newlines. HTMLMinifier offers a variety of optimisation options in the plugin that can be selected as per the user requirement.

Steps to use HTMLMinifier:

  1. Download the HTMLMinifier file from https://www.terresquall.com/download/HTMLMinifier.php
  2. Include the following code into the php file
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // Import the HTMLMinifier
    require_once 'myfolder/HTMLMinifier.php';
      
    // HTML source to be minified
    $htmlpage = file_get_contents('./mypage.html');
      
    // Minified version of the page
    echo HTMLMinifier::process($htmlpage);
      
    ?> 

    chevron_right

    
    

  3. Run the php file



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.