Results 1 to 6 of 6
  1. #1

    HTML compression in PHP

    Hi!

    Has anyone used a tool called Absolute HTML compressor? It's a windoze program that reduces a HTML file down by 10-20%, by removing extra white space, and unecessary new lines.

    I was wondering if anyone has used or developed a PHP function that achives a similar result?

    Cheers,
    Infinite
    Do'h!

  2. #2
    If you ad ob_gzhandler to yout php.ini all PHP prased pages will be automatially compressed by gzip if the clients browser supports it.

    ; You can redirect all of the output of your scripts to a function. For
    ; example, if you set output_handler to "ob_gzhandler", output will be
    ; transparently compressed for browsers that support gzip or deflate encoding.
    ; Setting an output handler automatically turns on output buffering.
    ;output_handler =
    output_handler = ob_gzhandler

  3. #3
    Originally posted by Starhost
    If you ad ob_gzhandler to yout php.ini all PHP prased pages will be automatially compressed by gzip if the clients browser supports it.

    Thanks for the tip Starhost , but gzip compression is already enabled, I'd just like to see if I can compress the HTML before gzip does, so if the browser won't support gzip, it will still be slightly compressed

    Also, I guess gzip would compress a HTML page more, that's been compressed/optimised already?

    Cheers,
    Infinite
    Do'h!

  4. #4
    Join Date
    Jul 2002
    Posts
    153
    Hmm thats interesting...Id like to know if anyone has used this tool as well.

  5. #5
    This is what I've got so far, it removes line returns, and multiple white space.
    Would anyone know how to remove HTML comment tags?
    Code:
    <?php
    
    function callback($buffer) {
    
    // Strip newlines and extra white space,
    // not yet sensitive to textarea tags
    
    $search = array ("'([\r\n])+'",         // Strip out newlines
                     "'[\s][\s]'"); // Strip out multiple white spaces
    
    $replace = array ("",
                      "",
                      "");
    
    return ( preg_replace ($search, $replace, $buffer) );
    
    
    }
    
    ob_start("callback");
    
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <body>
    Test    page 
    <BR>
    
    
    <BR>
    
    
    ho ho ho    !</body>
    
    
    </html>
    <?php
    
    ob_end_flush();
    
    ?>
    I've had a go at removing <!-- --> tags, but with no result so far, any help is much appreciated

    The above code brings my homepage down by 3%, not much, but the results will be much better on larger pages

    TIA,
    Infinite
    Do'h!

  6. #6
    Originally posted by esdjco
    Hmm thats interesting...Id like to know if anyone has used this tool as well.
    The tool actually does more than I want, this is what is listed in the help file
    • Merge lines.
      Remove unnecessary whitespace characters.
      Remove unnecessary quotation marks. For example, <table BORDER="0" CELLSPACING="0" CELLPADDING="0" align="center"> will be converted to <table BORDER=0 CELLSPACING=0 CELLPADDING=0 align=center>.
      Replace some tags with the short ones - replace STRIKE tags with S, STRONG with B and EM with I.
      Remove HTML comments. The comments within the scripts and styles aren't removed.
      Remove <!DOCTYPE ...> tags.
      Remove meta tags. You can specify names of the meta tags to remove.
      Create backups. If this option is ON backups of the optimized files are created in a new folder.


    But I don't want to remove all of these, just line returns and white space (and HTML comments)

    Cheers,
    Infinite
    Do'h!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •