PHP code for removing unnecessary whitespaces from html

Removing unnecessary white-spaces and extra characters from html source code, helps for improving overall rendering speed of web page. Specially in browser window. Following is simple PHP script which will remove all unnecessary white-spaces from html source. And thus minify it and improves rendering speed. Add this code in header of your PHP page to see the effect.

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);   $buffer = preg_replace('/<!--(.|\s)*?-->/', '', $buffer );    return $buffer;}ob_start("sanitize_output");?>



Popular posts from this blog

MATLAB code for Circular Convolution using Matrix method

Positive number pipe in angular 2+