Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rlandas/c49037c7ef6ad01a859a3bc5f56b1f49 to your computer and use it in GitHub Desktop.
Save rlandas/c49037c7ef6ad01a859a3bc5f56b1f49 to your computer and use it in GitHub Desktop.
PHP - remove leading and trailing spaces in HTML
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