title: Minifying final HTML output using regular expressions with CodeIgniter
body: <p>Google pages suggest you to minify HTML, that is, remove all the unnecessary spaces.
  <a href="http://en.wikipedia.org/wiki/Codeigniter#CodeIgniter" rel="noreferrer">CodeIgniter</a> does have the feature of giziping output or it can be done via <code>.htaccess</code>.
  But still I also would like to remove unnecessary spaces from the final HTML output as well.</p>
  
  <p>I played a bit with this piece of code to do it, and it seems to work. 
  This does indeed result in HTML that is without excess spaces and removes other tab formatting.</p>
  
  <pre><code>class Welcome extends CI_Controller 
  {
      function _output()
      {
          echo preg_replace('!\s+!', ' ', $output);
      }
  
      function index(){
      ...
      }
  }
  </code></pre>
  
  <p>The problem is there may be tags like
  <code>&lt;pre&gt;</code>,<code>&lt;textarea&gt;</code>, etc.. which may have spaces in them and a regular expression should remove them.
  So, how do I remove excess space from the final HTML, without effecting spaces or formatting for these certain tags using a regular expression?</p>
  
  <p>Thanks to @Alan Moore got the answer, this worked for me</p>
  
  <pre><code>echo preg_replace('#(?ix)(?&gt;[^\S ]\s*|\s{2,})(?=(?:(?:[^&lt;]++|&lt;(?!/?(?:textarea|pre)\b))*+)(?:&lt;(?&gt;textarea|pre)\b|\z))#', ' ', $output);
  </code></pre>
  
  <p>ridgerunner did a very good job of analyzing this regular expression. I ended up using his solution. Cheers to ridgerunner.</p>