HTML compression // white space removal


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

i know these regex stuff is rather slow, but still i like having slim documents. (don't forget to use gzip compression on all text docs)


Copy this code and paste it in your HTML
  1. /**
  2.  * @param $string (X)HTML document string
  3.  * @return string
  4.  * @todo benchmark/optimize performance
  5.  */
  6. private function minimizeHTML($string) {
  7. // will remove tabs, line breaks and extra white spaces
  8. $r = preg_replace(array('/\r/','/\n/','/
  9. /','/\t/'),'',$string);
  10. $r = preg_replace('/\s\s+/',' ',$out);
  11. return $r;
  12. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.