mirror of
https://github.com/bcit-ci/CodeIgniter.git
synced 2025-02-20 11:13:29 +08:00
Updated Compress HTML output (markdown)
parent
976fcb5f25
commit
8672a3a1f8
@ -1,4 +1,70 @@
|
||||
To remove useless whitespace from generated HTML, define a 'display_override' hook:
|
||||
To remove useless whitespace from generated HTML, except for Javascript. [[Regex Source](http://stackoverflow.com/questions/5312349/minifying-final-html-output-using-regular-expressions-with-codeigniter)]
|
||||
|
||||
### Step 1: Enable Hooks in config/config.php
|
||||
```php
|
||||
$config['enable_hooks'] = TRUE;
|
||||
```
|
||||
|
||||
### Step 2: Add in the compress hook to config/hooks.php
|
||||
```php
|
||||
// Compress output
|
||||
$hook['display_override'][] = array(
|
||||
'class' => '',
|
||||
'function' => 'compress',
|
||||
'filename' => 'compress.php',
|
||||
'filepath' => 'hooks'
|
||||
);
|
||||
```
|
||||
|
||||
### Step 3: define a 'display_override' hook:
|
||||
|
||||
```php
|
||||
<?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:
|
||||
|
||||
```php
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user