Created
April 4, 2016 01:14
-
-
Save rlandas/c49037c7ef6ad01a859a3bc5f56b1f49 to your computer and use it in GitHub Desktop.
PHP - remove leading and trailing spaces in HTML
This file contains 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
final class Html | |
{ | |
static public function removeComments($content = '') | |
{ | |
return preg_replace('/<!--(.|\s)*?-->/', '', $content); | |
} | |
static public function sanitize($content) | |
{ | |
$search = array( | |
// strip whitespaces after tags, except space | |
'/\>[^\S ]+/s', | |
// strip whitespaces before tags, except space | |
'/[^\S ]+\</s', | |
// shorten multiple whitespace sequences | |
'/(\s)+/s' | |
); | |
$replace = array( | |
'>', | |
'<', | |
'\\1' | |
); | |
$content = preg_replace($search, $replace, $content); | |
return static::removeComments($content); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment