How to Compress HTML Code Using PHP?

Compress HTML Code With PHP, How to Compress HTML Code by PHP Script Code,How to minify php page html output, PHP Function to Minify HTML, CSS and JavaScript, Minify HTML with PHP, How to use PHP to minify HTML output

Hello friends, today I will tell you through this tutorial how you can compress your html page by php. And what are the benefits after compressing the code?

If the code of html is compressed then the speed of our website is ever faster and loads very quickly on the browser.Friends, you must have seen that whatever the top website or quite a famous website, their code is always compressed.

You will be told to step to step how you can compress your html page via php code. So let’s go.

Overview

Step 1:- First of all, you create a php file. (Compresshtml.php)

Step 2:- Paste the php code below in this php file.

Step 3:- Open on Browser.

Step 1:- First of all, you create a php file. (Compresshtml.php)

Step 2:- Paste the php code below in this php file.

Compresshtml.php

<?php
ob_start("compress_htmlcode");
function compress_htmlcode($codedata) 
{
$searchdata = array(
'/\>[^\S ]+/s', // remove whitespaces after tags
'/[^\S ]+\</s', // remove whitespaces before tags
'/(\s)+/s' // remove multiple whitespace sequences
);
$replacedata = array('>','<','\\1');
$codedata = preg_replace($searchdata, $replacedata, $codedata);
return $codedata;
}
?>

<!DOCTYPE html>
<html>
<head>
	<title>How to Compress HTML Code Using PHP?</title>
</head>
<body>
<h2>How to Compress HTML Code Using PHP?</h2>
<form action="/action_page.php">
  First name:<br>
  <input type="text" name="firstname" value="Mickey">
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse">
  <br><br>
  <input type="submit" value="Submit">
</form> 
<p>If you click the "Submit" button, the form-data will be sent to a page called "/action_page.php".</p>
</body>
</html>
<?php
ob_end_flush();
?>

 

Step 3:- Then you can see the output of the source code by opening it on your server.
etc…. http://localhost/compress/compresshtml.php