forked from bcit-ci/CodeIgniter
-
Notifications
You must be signed in to change notification settings - Fork 0
Compress HTML output
Derek Jones edited this page Jul 5, 2012
·
5 revisions
To remove useless whitespace from generated HTML, define a 'display_override' hook:
$CI =& get_instance();
$buffer = $CI->output->get_output();
$search = array(
'/\>[^\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();
Compatible with CI caching mechanism (compressed HTML is cached).
Same thing but with HTML Tidy (PHP 5 only):
$CI =& get_instance();
$buffer = $CI->output->get_output();
$options = array(
'clean' => true,
'hide-comments' => true,
'indent' => true
);
$buffer = tidy_parse_string($buffer, $options, 'utf8');
tidy_clean_repair($buffer);
// warning: if you generate XML, HTML Tidy will break it (by adding some HTML: doctype, head, body..) if not configured properly
$CI->output->set_output($buffer);
$CI->output->_display();
Reference: http://maestric.com/en/doc/php/codeigniter_compress_html
Jérôme Jaglale