The code defines a PHP function named “minifier” which minifies HTML code. It does so by using the PHP preg_replace function to search for certain patterns in the input HTML code and replace them with simpler, optimized versions. The function takes an HTML string as input and returns a minified version of the same HTML string.
The $search
array defines the patterns to look for in the HTML code, which are:
- Remove whitespaces after tags (
/\>[^\S ]+/s
) - Remove whitespaces before tags (
/[^\S ]+\</s
) - Remove multiple whitespace sequences (
/(\s)+/s
) - Remove HTML comments (
/<!--(.|\s)*?-->/
)
The $replace
array defines what to replace the patterns in $search
with. The replacements are:
- Replace whitespaces after tags with
>
- Replace whitespaces before tags with
<
- Replace multiple whitespace sequences with a single space (
\\1
)
Finally, the function uses preg_replace
to replace the patterns in $search
with the corresponding replacements in $replace
, effectively minifying the input HTML code.
Main Code
<?php
//Minify Main HTML - OPTIMIZATION TESTED By https://CarlosBaez.Dev
ob_start("minifier");
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;
}
?>
How To Apply In Real World Scenario
The real-world scenario simply requires that your PHP code sits at the front of your HTML DOC and the use of ob_end_flush() to finalize everything. Everything in between <html>and </html> is where you would place your code. What OB_start does, is “render” the page before it shows it to the user, so you can modify its output. If you have never used ob_start before, I will be writing a quick guide later on about this, but for now, feel free to visit PHP’s code explanation for it here: PHP.NET OB_START
<?php
//Minify Main HTML - OPTIMIZATION TESTED By https://CarlosBaez.Dev
ob_start("minifier");
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;
}
?>
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:title" content="">
<meta property="og:type" content="">
<meta property="og:url" content="">
<meta property="og:image" content="">
<link rel="manifest" href="site.webmanifest">
<link rel="apple-touch-icon" href="icon.png">
<!-- Place favicon.ico in the root directory -->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<meta name="theme-color" content="#fafafa">
</head>
<body>
<!-- Add your site or application content here -->
<p>Hello world! This is HTML5 Boilerplate.</p>
<script src="js/vendor/modernizr-3.11.2.min.js"></script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>
<!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
<script>
window.ga = function () { ga.q.push(arguments) }; ga.q = []; ga.l = +new Date;
ga('create', 'UA-XXXXX-Y', 'auto'); ga('set', 'anonymizeIp', true); ga('set', 'transport', 'beacon'); ga('send', 'pageview')
</script>
<script src="https://www.google-analytics.com/analytics.js" async></script>
</body>
</html>
<?php
ob_end_flush();
?>
Have A Developer Website and Don’t Need to Minify Pre & Code?
Use this code instead so that pre & code are ignored during the minification process.
//Minify Main HTML - OPTIMIZATION TESTED by https://CarlosBaez.Dev
ob_start("minifier");
function minifier($code) {
$search = array(
// Remove whitespaces after tags
'/\>[^\S ]+/s',
// Remove whitespaces before tags
'/[^\S ]+\</s',
// Remove multiple whitespace sequences
'/(\s)+/s'
);
$replace = array('>', '<', '\\1');
// Match all "pre" and "code" tags
preg_match_all('/<(pre|code)>(.*?)<\/(pre|code)>/s', $code, $matches);
// Replace all "pre" and "code" tags with placeholders
$code = preg_replace('/<(pre|code)>(.*?)<\/(pre|code)>/s', '[[pre_code]]', $code);
// Perform minification on the code excluding "pre" and "code" tags
$code = preg_replace($search, $replace, $code);
// Replace the placeholders with the original "pre" and "code" tags
foreach ($matches[0] as $match) {
$code = preg_replace('/\[\[pre_code\]\]/', $match, $code, 1);
}
return $code;
}