Skip to content

Instantly share code, notes, and snippets.

@wayneashleyberry
Created December 19, 2012 22:52
Show Gist options
  • Save wayneashleyberry/4341314 to your computer and use it in GitHub Desktop.
Save wayneashleyberry/4341314 to your computer and use it in GitHub Desktop.
minify html
<?php
class HTML {
/**
* Remove comments and whitespace
*/
public static function minify($content)
{
$content = static::strip_comments($content);
return static::strip_whitespace($content);
}
public static function strip_whitespace($content)
{
$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'
);
return preg_replace($search, $replace, $content);
}
public static function strip_comments($content)
{
return preg_replace("/<!--[^>]*-->/", '', $content);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment