Created
December 19, 2012 22:52
-
-
Save wayneashleyberry/4341314 to your computer and use it in GitHub Desktop.
minify html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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