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