Minify HTML in CodeIgniter using Hooks

Are you trying to minify HTML in Codeigniter? Then this article is for you.

Before starting, lets discuss briefly on Minification.


CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications. CodeIgniter is loosely based on the popular model–view–controller (MVC) development pattern.

HTML Minification

HTML Minification is the removal of unnecessary characters and lines in HTML code. Indentation, comments, empty lines, etc. are not required while rendering in HTML. Trimming these details can save download file size not only without affecting the output but also improving the performance.

Sample HTML without Minification

    <title>Your Title Here</title>
      <img src="clouds.jpg" ALIGN="BOTTOM">
    <a href="">Link Name</a> is a link to another nifty site
    <h1>This is a Header</h1>
    <h2>This is a Medium Header</h2>
    Send me mail at <a href=""></a>
    <p> This is a new paragraph!
        <b>This is a new paragraph!</b>
        <b><i>This is a new sentence without a paragraph break, in bold italics.</i></b>


HTML after Minification

<html><head><title>Your Title Here</title></head><body><center><img SRC="clouds.jpg" ALIGN="BOTTOM"></center><hr><a href="">Link Name</a> is a link to another nifty site<h1>This is a Header</h1><h2>This is a Medium Header</h2>Send me mail at <a href=""></a><p> This is a new paragraph!<p><b>This is a new paragraph!</b><br><b><i>This is a new sentence without a paragraph break, in bold italics.</i></b></p></p></body></html>


Steps to Minify HTML in Codeigniter


Step 1 : Enable Hooks in Config

Go to application/config/config.php and make sure there exists the line

$config['enable_hooks'] = TRUE;


Step 2 : Declare a Hook in hooks.php

Go to application/config/hooks.php and declare a hook like this:

// compress output
$hook['display_override'][] = array(
  'class' => '',
  'function' => 'compress',
  'filename' => 'compress.php',
  'filepath' => 'hooks'


Step 3 : Add the hook

Create a file application/hooks/compress.php with the following code in it:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function compress()
  $CI =& get_instance();
  $buffer = $CI->output->get_output();
   $search = array(
    '/\n/',			// replace end of line by a space
    '/\>[^\S ]+/s',		// strip whitespaces after tags, except space
    '/[^\S ]+\</s',		// strip whitespaces before tags, except space
   	'/(\s)+/s'		// shorten multiple whitespace sequences
   $replace = array(
    ' ',
  $buffer = preg_replace($search, $replace, $buffer);
/* End of file compress.php */
/* Location: ./system/application/hooks/compress.php */

That’s it ! Now the HTML rendered with be minified in our webpage.

You may also like: Page Caching to Improve Website Performance


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.