Bladephp: Now with 8+ codeigniter, php, and laravel project 8+ php project From $37/Month From Rs. 2499/Month

How to Compress HTML Code using PHP

by
Category:php
php html
How to Compress HTML Code using PHP

Compressing the code is very beneficial for a website it increases website performance and load it faster by making webpage smaller and lighter and it also affects website SEO, so this script strips out all the line breaks and spaces in your code and puts it on one line, compressing your code and making it faster. You may also like How to Disable AutoComplete Form using jQuery.

PHP and HTML Code

<?php
ob_start("compress_code");

function compress_code($code) {
    $search = array(
        '/\>[^\S ]+/s',
        '/[^\S ]+\</s',
        '/(\s)+/s'
    );

    $replace = array('>', '<', '\\1');
    $code = preg_replace($search, $replace, $code);
    return $code;
}
?>
<html>
    <body>
        <p>How to Compress HTML Code using PHP</p>
    </body>
</html>

<?php
ob_end_flush();
?>

0 Comments


Html code work in comment box


Related Post