Minify HTML using PHP

mins

HTML Minification is performed after the code for a web application is written, but before the application is deployed. When a user requests a webpage, the minified version is sent instead of the full version, resulting in faster response times and lower bandwidth costs. HTML Minification works by analysing and rewriting the text-based parts of a website to reduce its overall file size.

When web developers are creating sites, they use all kinds of extra characters, comments and notes for their use in developing pages. The problem is, the search engine crawlers don't need all of that, and all the extra stuff slows down the page load speed time, which is not good for the web user, and Google constantly tells you through Search Console that dammit, you need to speed those web pages up!

But fear not, this is a problem that is very easy to fix. Just add the following code to each page of your site. Job done !

<?php
function sanitize_output($buffer) {
$search = array(
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences
'/<!--(.|\s)*?-->/' // Remove HTML comments
);
$replace = array(
'>',
'<',
'\\1',
''
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
ob_start("sanitize_output");
?>

So now you know what minification is, why it impacts your SEO efforts, you are an SEO monkey!

I do not use cookies on this website

🍪🍪 What cookies?


Your privacy is important. That's why this website doesn't track visitors with fingerprinting, your IP address or cookies.


Web analytics data is your own. Reclaim it.

Gain back control with a privacy-focused web analytics platform (using a cookie-less architecture) from: Data Centurion.