Skip to content

Instantly share code, notes, and snippets.

@natanfelles
Last active March 1, 2018 11:25
Show Gist options
  • Save natanfelles/ed62817cb253537f2c7d64648c2bb806 to your computer and use it in GitHub Desktop.
Save natanfelles/ed62817cb253537f2c7d64648c2bb806 to your computer and use it in GitHub Desktop.
Editing Compress HTML output

Source: https://github.com/bcit-ci/CodeIgniter/wiki/Compress-HTML-output

To remove useless whitespace from generated HTML, except for Javascript see [Regex Source] or just replace the $re value:

$re = '%# Collapse ws everywhere but in blacklisted elements.
        (?>             # Match all whitespaces other than single space.
          [^\S ]\s*     # Either one [\t\r\n\f\v] and zero or more ws,
        | \s{2,}        # or two or more consecutive-any-whitespace.
        ) # Note: The remaining regex consumes no text at all...
        (?=             # Ensure we are not in a blacklist tag.
          (?:           # Begin (unnecessary) group.
            (?:         # Zero or more of...
              [^<]++    # Either one or more non-"<"
            | <         # or a < starting a non-blacklist tag.
              (?!/?(?:textarea|pre)\b)
            )*+         # (This could be "unroll-the-loop"ified.)
          )             # End (unnecessary) group.
          (?:           # Begin alternation group.
            <           # Either a blacklist start tag.
            (?>textarea|pre)\b
          | \z          # or end of file.
          )             # End alternation group.
        )  # If we made it here, we are not in a blacklist tag.
        %ix';

Step 1: Enable Hooks in config/config.php

$config['enable_hooks'] = TRUE;

Step 2: Add in the compress hook to config/hooks.php

// Compress output
$hook['display_override'][] = array(
	'class' => '',
	'function' => 'compress',
	'filename' => 'compress.php',
	'filepath' => 'hooks'
);

Step 3: define a 'display_override' hook:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function compress()
{
	ini_set("pcre.recursion_limit", "16777");
	$CI =& get_instance();
	$buffer = $CI->output->get_output();

	$re = '%# Collapse whitespace everywhere but in blacklisted elements.
        (?>             # Match all whitespans other than single space.
          [^\S ]\s*     # Either one [\t\r\n\f\v] and zero or more ws,
        | \s{2,}        # or two or more consecutive-any-whitespace.
        ) # Note: The remaining regex consumes no text at all...
        (?=             # Ensure we are not in a blacklist tag.
          [^<]*+        # Either zero or more non-"<" {normal*}
          (?:           # Begin {(special normal*)*} construct
            <           # or a < starting a non-blacklist tag.
            (?!/?(?:textarea|pre|script)\b)
            [^<]*+      # more non-"<" {normal*}
          )*+           # Finish "unrolling-the-loop"
          (?:           # Begin alternation group.
            <           # Either a blacklist start tag.
            (?>textarea|pre|script)\b
          | \z          # or end of file.
          )             # End alternation group.
        )  # If we made it here, we are not in a blacklist tag.
        %Six';

    $new_buffer = preg_replace($re, " ", $buffer);

    // We are going to check if processing has working
	if ($new_buffer === null)
	{
		$new_buffer = $buffer;
	}

	$CI->output->set_output($new_buffer);
	$CI->output->_display();
}
 
/* End of file compress.php */
/* Location: ./system/application/hooks/compress.php */

Old Method:

$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

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