Skip to content

Instantly share code, notes, and snippets.

@abedputra
Last active November 17, 2023 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save abedputra/2d2527304019ee54b1f2548598d34340 to your computer and use it in GitHub Desktop.
Save abedputra/2d2527304019ee54b1f2548598d34340 to your computer and use it in GitHub Desktop.
Compress HTML output with CodeIgniter
<?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',
"//&lt;![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
?>
<?php
//------system/application/config/config.php
$config['enable_hooks'] = TRUE;
?>
<?php
//------system/application/config/hooks.php
// compress output
$hook['display_override'][] = array(
'class' => '',
'function' => 'compress',
'filename' => 'compress.php',
'filepath' => 'hooks'
);
?>
@allanrosero
Copy link

how can i use this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment