HTML Compressor with regex I would like to compress a Magento HTML page using some regex, and this is what I have written:

function html_compress($string){

    global $idarray;
    $idarray=array();

    //Replace PRE and TEXTAREA tags
    $search=array(
                    '@(&lt;)\s*?(pre\b[^&gt;]*?)(&gt;)([\s\S]*?)(&lt;)\s*(/\s*?pre\s*?)(&gt;)@',   //Find PRE Tag
                    '@(&lt;)\s*?(textarea\b[^&gt;]*?)(&gt;)([\s\S]*?)(&lt;)\s*?(/\s*?textarea\s*?)(&gt;)@' //Find TEXTAREA
                );
    $string=preg_replace_callback($search,
                                    function($m){
                                        $id='&lt;!['.uniqid().']!&gt;';
                                        global $idarray;
                                        $idarray[]=array($id,$m[0]);
                                        return $id;
                                    },
                                    $string
    );

    //Remove blank useless space
    $search = array(
                    '@( |\t|\f)+@', // Shorten multiple whitespace sequences
                    '@(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+@',   //Remove blank lines
                    '@^(\s)+|( |\t|\0|\r\n)+$@' //Trim Lines
                    );
    $replace = array(' ',"\\1",'');
    $string = preg_replace($search, $replace, $string);

    //Replace IE COMMENTS, SCRIPT, STYLE and CDATA tags
    $search=array(
                    '@&lt;!--\[if\s(?:[^&lt;]+|&lt;(?!!\[endif\]--&gt;))*&lt;!\[endif\]--&gt;@',  //Find IE Comments
                    '@(&lt;)\s*?(script\b[^&gt;]*?)(&gt;)([\s\S]*?)(&lt;)\s*?(/\s*?script\s*?)(&gt;)@',    //Find SCRIPT Tag
                    '@(&lt;)\s*?(style\b[^&gt;]*?)(&gt;)([\s\S]*?)(&lt;)\s*?(/\s*?style\s*?)(&gt;)@',  //Find STYLE Tag
                    '@(//&lt;!\[CDATA\[([\s\S]*?)//]]&gt;)@', //Find commented CDATA
                    '@(&lt;!\[CDATA\[([\s\S]*?)]]&gt;)@'  //Find CDATA
                );
    $string=preg_replace_callback($search,
                                    function($m){
                                        $id='&lt;!['.uniqid().']!&gt;';
                                        global $idarray;
                                        $idarray[]=array($id,$m[0]);
                                        return $id;
                                    },
                                    $string
    );

    //Remove blank useless space
    $search = array(
                    '@(class|id|value|alt|href|src|style|title)=(\'\s*?\'|"\s*?")@',    //Remove empty attribute
                    '@&lt;!--([\s\S]*?)--&gt;@',  // Strip comments except IE
                    '@[\r\n|\n|\r]@', // Strip break line
                    '@[ |\t|\f]+@', // Shorten multiple whitespace sequences
                    '@(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+@', //Remove blank lines
                    '@^(\s)+|( |\t|\0|\r\n)+$@' //Trim Lines
                    );
    $replace = array(' ','',' ',' ',"\\1",'');
    $string = preg_replace($search, $replace, $string);

    //Replace unique id with original tag
    $c=count($idarray);
    for($i=0;$i&lt;$c;$i++){
        $string = str_replace($idarray[$i][0], "\n".$idarray[$i][1]."\n", $string);
    }

    return $string;
}


It works, but I have got some concerns:


Has it got sense to explit all the \s*? between the tags and
retrieve with this (&lt;)\s*?(style\b[^&gt;]*?)(&gt;)?
Does this script eat resources and considerably delay the page load? Is there any possible optimization?
Is the part that remove the white space redundant?
Is all of this "cacheable"?
See: https://stackoverflow.com/a/6225706/736079

For comments on enabling content compression for your HTML pages, that usually is enough to reduce the payload by more than 50%.

You can also make use of output buffering and combine it with the HTMLMinify function:



&lt;?php
function sanitize_output($content) {
     $content = Minify_HTML::minify($content);
}

ob_start("sanitize_output");
?&gt;


See: https://github.com/mrclay/minify/blob/master/min/lib/Minify/HTML.php

This still uses Regex at the core, which is still not ideal, but it has been tested by a larger audience and looks quite solid (test to make sure). If you are hosting on IIS, you might be able to use a .NET HttpModule or an ISAPI filter as well. This isn't limited to PHP only, sometimes the Web Server itself has plugins that can help you, like Apache's mod_pagespeed.