You are here

function minifyhtml_minify_html in Minify Source HTML 7

Helper function to minify the HTML.

Parameters

string $page: The HTML source of the page.

1 call to minifyhtml_minify_html()
minifyhtml_minify in ./minifyhtml.module
Helper function to minify HTML.

File

./minifyhtml.module, line 371
Hook and helper functions for the Minify HTML module.

Code

function minifyhtml_minify_html(&$page) {
  $search = array();
  $replace = array();

  // Remove whitespaces after tags, except space.
  $search[] = '/\\>[^\\S ]+/s';
  $replace[] = '>';

  // Remove whitespaces before tags, except space.
  $search[] = '/[^\\S ]+\\</s';
  $replace[] = '<';

  // Shorten multiple whitespace sequences.
  $search[] = '/(\\s)+/s';
  $replace[] = '\\1';

  // Remove whitespaces around block/undisplayed elements.
  $search[] = '/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body' . '|caption|center|col(?:group)?|dd|dir|div|dl|dt|fieldset|form' . '|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta' . '|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h||r|foot|itle)' . '|ul)\\b[^>]*>)/i';
  $replace[] = '$1';

  // Trim each line.
  $search[] = '/^\\s+|\\s+$/m';
  $replace[] = '';
  $page = preg_replace($search, $replace, $page);
}