Optimized Page Output function

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

I was just playing around this morning and came up with the following function to output a webpage whose code is stored in a variable, pretty nice if you are using your own CMS/Templating code. (Smarty has something similar to this).

It will strip out all HTML comments, and preserve any <pre>...</pre> code's spacing. To make it slightly "readable" I have it set to word wrap the lines echoed out (except the <pre>...</pre> chucks) to 120 characters. and the pre's get started on their own lines.

Use the second version if you just want as minimal as possible.

At first I didn't use the check to see if the chunk was a <pre> chunk, I was just alternating back and forth, however realized if you put two <pre> sections back to back, although probably not likely to happen, it would break it, so used the check on each chunk instead.

Let me know if you have any questions comments.

-Greg

function echoComplressHTML(&$html){
    $code = $result = preg_replace('/<!--\s.+?\s-->/s', '', $html); // Strip HTML Comments
    $aryHtmlChunk = preg_split('%(<pre>.+?</pre>)%si',$code,0,PREG_SPLIT_DELIM_CAPTURE);
    $bLastWasPre = false;
    foreach($aryHtmlChunk as $intNum=>$code) {
        if (strtolower(substr($code,0,5))=='<pre>') {
            if (!$bLastWasPre) echo "\n";
            echo $code."\n";
            $bLastWasPre = true;
        } else {
            $code = trim(preg_replace('/\s+/',' ',$code));
            if ($code!='') {
                echo wordwrap($code,120);
                $bLastWasPre = false;
            }
        }
    }
}

function echoComplressHTML(&$html){
    $code = $result = preg_replace('/<!--\s.+?\s-->/s', '', $html); // Strip HTML Comments
    $aryHtmlChunk = preg_split('%(<pre>.+?</pre>)%si',$code,0,PREG_SPLIT_DELIM_CAPTURE);
    foreach($aryHtmlChunk as $intNum=>$code) {
        if (strtolower(substr($code,0,5))=='<pre>') {
            echo $code."\n";
        } else {
            echo trim(preg_replace('/\s+/',' ',$code));
        }
    }
}

They have: 121 posts

Joined: Dec 2008

Thank you for sharing - it is always interesting to see how others approach requirements such as this...

I've had good luck doing similar with the html tidy extension in PHP:
http://ca.php.net/manual/en/book.tidy.php

Though - I'm not quite sure what it would do to contents within the pre tags - some experimentation is likely necessary with the different options / flags available here:

http://tidy.sourceforge.net/docs/quickref.html

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Sweet! I'll have to give that a try! Thank you for the link to that.

-Greg

They have: 33 posts

Joined: Apr 2010

Greg! An important thing to note is that you should not use such stripping Smiling

The following line is crucial:

$code = $result = preg_replace('/<!--\s.+?\s-->/s', '', $html); // Strip HTML Comments

It will make your markup incompatible with many browsers. How? I will explain. Suppose your site is not appearing fine in IE6 , but it is fine on all other browsers. So, you need to ask IE6 to do something extra to make your page look atleast acceptible. You can give such instructions inside <!-- -->. This is called conditional comments. You can incluse CSS files, JS files, Apply CSS, or anything you want.

Here is a detailed post on this topic (specifically on conditional comments): http://www.quirksmode.org/css/condcom.html

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.