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(
		' ',
		'>',
		'<',
		'\\1'
	);
	$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

Author: Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.
I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution to problems which i already solved.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts: You may like

Online Directory Software – Local Business Directories Application

Pagination in CodeIgniter With Search Query

Codeigniter pagination with search query string

Redirect http to https using htaccess

How to execute mysql query inside codeigniter helper

Upload and Resize Image and Create Thumbnail Codeigniter

Rename file while uploading in codeigniter

Pagination In CodeIgniter With Example

Upload file with database codeigniter

Simple codeigniter login system

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now

CURRENCY CONVERTER

Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now

ROBOST.TXT GEN.

Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now

HTML MINIFIER

Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now

CSS MINIFIER

Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now

JAVASCRIPT MINIFIER

Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

Minify Now

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.