if you are looking for how to minify HTML in Codeigniter? Then this tutorial is for you.
Before starting, lets discuss briefly on 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.
<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><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
Go to application/config/config.php and make sure inside config.php file $config[‘enable_hooks’] Shoild be true.
$config['enable_hooks'] = TRUE;
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' );
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 & JavascriptMy 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.