Skip to content

Instantly share code, notes, and snippets.

@adamtester
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save adamtester/3040d38ee5d3fd6bc7da to your computer and use it in GitHub Desktop.

Select an option

Save adamtester/3040d38ee5d3fd6bc7da to your computer and use it in GitHub Desktop.
Compress script for CodeIgniter
function compress()
{
ini_set("pcre.recursion_limit", "16777");
$CI =& get_instance();
$CI->benchmark->mark('compression_start');
// Set output type
$CI->output->set_header('Content-type: text/html; charset=utf-8');
$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->benchmark->mark('compression_end');
$CI->output->_display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment