1.Optimizing HTML codes

Introduction:
View the page source of your website, compare it to page source of facebook and google, what you observe? unreadable.:) It is because they are optimizing their html codes as our equation:

Browsing speed = Load faster = Happy user.

In codeigniter, LET’S DO THIS.

Easy Steps:

1.      Create a file(optimize.php) with a function to compress raw html as shown below:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function compress() {
                $CI =& get_instance();
                $raw_html = $CI->output->get_output();
                $search = array('/[\r\n\t]+/','/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s',);
                $replace = array(' ','>','<','\\1',);
                $compressed_html = preg_replace($search, $replace, $raw_html);
                $CI->output->set_output($compressed_html);
                $CI->output->_display();
}

2.     Copy the file (optimize.php) to the system/application/hooks folder.

3.     Reference the file(optimize.php)by adding this code to the system/application/config/hooks.php
$hook['display_override'][ ] = array(
        'filepath' => 'hooks'
        'filename' => 'optimize.php',
        'function' => 'compress',
        );

4.      Enable hook in the system/application/config/config.php
$config['enable_hooks'] = TRUE;

5.     View source on your page and witness the compression magic you made.:)

1 comment: