Back To Normal
Click Here To Subscribe Via Email

Subscribe To Our E-Mail Newsletter

Friday, November 14, 2014

PHP function for HTML minification


Couch ModePrint It



When it comes to coding a good web-app or a website using PHP, serving minified resources is a must have thing that you should consider implementing. Today, PageSpeed is an issue for some webmasters while optimizing PageSpeed is a usual practice for others. Here i am sharing a simple PHP function which can be used to minify HTML output easily. This is one of some best PHP code snippets :)

I have found this piece of code on the web. This function works great but there are some rare cases (if you are using PRE tags or so, this function may remove newlines in it too).

The PHP Function for HTML minification


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
    );

    $replace = array(
        '>',
        '<',
        '\\1'
    );

    $buffer = preg_replace($search, $replace, $buffer);

    return $buffer;
}



You will need to place this function in your PHP file (right after the PHP starting tag -- <?php). For example, i have this function in my header.php file that is included on top of all PHP files.

So, let me define what this code will actually do when you use it. This code searches for whitespaces before and after html tags and then remove them. It also replaces multiple white spaces with single one.

Basic Usage


/* example html string */
$html=<<<EOF

<!DOCTYPE html>

<html>

           <head>

                      <title>Test page<title>

           </head>

           <body>

                      <h1>Welcome!</h1>

           </body>

</html>

EOF;

/* minify html */
$html_minified=sanitize_output($html);

/* send minified output */
echo $html_minified;



This is a simple example of using the php function for removing extra characters from the html. Demo

Minify HTML using ob_start()


You can minify your PHP website's output automatically by using ob_start(). Place the code that defines the PHP function sanitize_output() on top of your PHP file and then place the following line after that function code.
ob_start("sanitize_output");


So, the full code may look like this. Demo
<?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
    );

    $replace = array(
        '>',
        '<',
        '\\1'
    );

    $buffer = preg_replace($search, $replace, $buffer);

    return $buffer;
}

ob_start("sanitize_output");

/* regular html is started from here */
?>

<!DOCTYPE html>

<html>

           <head>

                      <title>Test page<title>

           </head>

           <body>

                      <h1>Welcome!</h1>

           </body>

</html>




I hope that you like this :)



:D
:)
:[
;)
:D
:O
(6)
(A)
:'(
:|
:o)
8)
(K)
(M)