Skip to content

Instantly share code, notes, and snippets.

@ArbahudRioDaroyni
Last active January 20, 2021 19:49
Show Gist options
  • Save ArbahudRioDaroyni/36fca684f298eba8214072556a1ab08f to your computer and use it in GitHub Desktop.
Save ArbahudRioDaroyni/36fca684f298eba8214072556a1ab08f to your computer and use it in GitHub Desktop.
<?php
function minifier($code) {
$search = array(
// Remove whitespaces after tags
'/\>[^\S ]+/s',
// Remove whitespaces before tags
'/[^\S ]+\</s',
// Remove multiple whitespace sequences
'/(\s)+/s',
// Removes comments
'/<!--(.|\s)*?-->/'
);
$replace = array('>', '<', '\\1');
$code = preg_replace($search, $replace, $code);
return $code;
}
?>
<?php ob_start("minifier"); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Page Title</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>
<script src='main.js'></script>
</head>
<body>
</body>
</html>
<?php ob_end_flush(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment