Minify the HTML that is outputted by PHP


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.