-
-
Save allanrosero/25b4fc731b2c2f48b3ce8df342f20383 to your computer and use it in GitHub Desktop.
Compress HTML output with CodeIgniter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
function compress() | |
{ | |
$CI =& get_instance(); | |
$buffer = $CI->output->get_output(); | |
$search = array( | |
'/\>[^\S ]+/s', | |
'/[^\S ]+\</s', | |
'/(\s)+/s', // shorten multiple whitespace sequences | |
'#(?://)?<!\[CDATA\[(.*?)(?://)?\]\]>#s' //leave CDATA alone | |
); | |
$replace = array( | |
'>', | |
'<', | |
'\\1', | |
"//<![CDATA[\n".'\1'."\n//]]>" | |
); | |
$buffer = preg_replace($search, $replace, $buffer); | |
$CI->output->set_output($buffer); | |
$CI->output->_display(); | |
} | |
/* End of file compress.php */ | |
/* Location: ./system/application/hooks/compress.php */ | |
//------system/application/hooks/compress.php | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//------system/application/config/config.php | |
$config['enable_hooks'] = TRUE; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//------system/application/config/hooks.php | |
// compress output | |
$hook['display_override'][] = array( | |
'class' => '', | |
'function' => 'compress', | |
'filename' => 'compress.php', | |
'filepath' => 'hooks' | |
); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment