Posted By

trusktr on 04/27/11


Tagged

php html compression minification


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

boksiora


Minify the HTML that is outputted by PHP


 / Published in: PHP
 

URL: http://ru.php.net/manual/en/function.ob-start.php#71953

Place this at the top of your php files. This will remove white space and formatting from HTML, making it smaller and faster for download by your users.

  1. <?php
  2.  
  3. function sanitize_output($buffer)
  4. {
  5. $search = array(
  6. '/\>[^\S ]+/s', //strip whitespaces after tags, except space
  7. '/[^\S ]+\</s', //strip whitespaces before tags, except space
  8. '/(\s)+/s' // shorten multiple whitespace sequences
  9. );
  10. $replace = array(
  11. '>',
  12. '<',
  13. '\\1'
  14. );
  15. $buffer = preg_replace($search, $replace, $buffer);
  16. return $buffer;
  17. }
  18.  
  19. ob_start("sanitize_output");
  20.  
  21. ?>

Report this snippet  

You need to login to post a comment.