Minify HTML in CodeIgniter using Hooks

Minify HTML in CodeIgniter using Hooks

if you are looking for how to minify HTML in Codeigniter? Then this tutorial is for you.

Before starting, lets discuss briefly on Minification.

What is HTML Minification

The term “minify” in programming language, It’s describes the processes of removing unnecessary characters in the source code. These characters include whitespaces, line breaks, comments, and block delimiters which are useful for developer but unnecessary for machines and it’s not affecting the output but also improving the performance of the website. We minify the files of a website containing CSS, HTML, and Javascript code so your web browser can read them faster.

Sample HTML without Minification

<html>
	<head>
		<title>Your Title Here</title>
	</head>
<body>
	<h1>Sample HTML without Minification</h1>
	<p>The term "minify" in programming language, It's describes the processes of removing unnecessary characters in the source code. These characters include whitespaces, line breaks, comments, and block delimiters which are useful for developer but unnecessary for machines and it's not affecting the output but also improving the performance of the website.</p>
	<p>We minify the files of a website containing CSS, HTML, and Javascript code so your web browser can read them faster.</p>
</body>
</html>

HTML after Minification

<html><head><title>Your Title Here</title></head><body><h1>What is HTML Minification</h1><p>The term "minify" in programming language, It's describes the processes of removing unnecessary characters in the source code. These characters include whitespaces, line breaks, comments, and block delimiters which are useful for developer but unnecessary for machines and it's not affecting the output but also improving the performance of the website.</p><p>We minify the files of a website containing CSS, HTML, and Javascript code so your web browser can read them faster.</p></body></html>
How to Minify HTML in Codeigniter Step by Step

Step 1 : Enable Hooks in Config

Go to application/config/config.php and make sure inside config.php file $config[‘enable_hooks’] Shoild be true.

$config['enable_hooks'] = TRUE;

Step 2 : Declare a Hook in hooks.php File

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 compress.php inside application/hooks/ Folder with the following code:

<?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);
	$CI->output->set_output($buffer);
	$CI->output->_display();
}
?>

HTML minify code is ready you can check Output of the HTML code will be minify you can run any page of your application and check HTML source code with CTR+U

Tags: codeigniter minify, php minify html, ci_minifier, display_override hook codeigniter, codeigniter obfuscator, compress components with gzip codeigniter, codeigniter speed optimization, compact in codeigniter, How to Speed Up CodeIgniter, Minify or Compress HTML Output with CodeIgniter, Minifying final HTML output using regular expressions, Compress HTML output with CodeIgniter, How to Minify Your Website CSS, HTML & Javascript
About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.