jesobreira

HTML output minifier for PHP

Feb 19th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. <?php
  2. /**
  3.  * HTML output minifier
  4.  * @author Jefrey <jefrey[at]jefrey[dot]ml>
  5.  * /
  6.  
  7. /*
  8. Include this code **at the end** of all your scripts that produces output you want to be minified (or use auto_prepend_file in .htaccess or php.ini, if supported by your host).
  9. This is supposed to be a simple solution, a trick/workaround (whatever you want to call this). If you want something more professional consider choosing a framework or dependency.
  10.  
  11. Known issues:
  12.  - Text between <textarea> and <code> will also lose tabs/new lines.
  13. */
  14. $buffer = ob_get_clean();
  15.  
  16. // remove duplicated spaces
  17. while(strpos($buffer, "  ")) {
  18.     $buffer = str_replace("  ", " ", $buffer);
  19. }
  20.  
  21. // remove tabulation
  22. $buffer = str_replace("\t", null, $buffer);
  23.  
  24. // remove newlines
  25. $buffer = str_replace("\n", null, $buffer);
  26. $buffer = str_replace("\r", null, $buffer);
  27.  
  28. echo $buffer;
Add Comment
Please, Sign In to add comment