如何在没有JavaScript错误的情况下将PHP输出发送到浏览器之前压缩它?

I have a function that compress the php output but it gives me problems with the inline javascript.

I found a page with related topic but the sample from there it's not work: http://jeromejaglale.com/doc/php/codeigniter_compress_html

The problem occur when use // shorten multiple whitespace sequences

If I remove this part from the array the Javascript is working well.

Question:

It's there a possibility to achieve the desired result without the need to use other libraries like Minify or Tidy?

The function:

/**
* [sanitize_output Compress the php output]
* @param  [type] $buffer [ Buffer = ob_get_contents(); ]
* @return [type]         [ string ]
*/
function sanitize_output($buffer)
{

$search = array(
'/\>[^\S ]+/s',         //strip whitespaces after tags, except space
'/[^\S ]+\</s',         //strip whitespaces before tags, except space
'/(\s)+/s',             // shorten multiple whitespace sequences
'/<!--(.|\s)*?-->/',    //strip HTML comments
'#(?://)?<!\[CDATA\[(.*?)(?://)?\]\]>#s', //leave CDATA alone

);
$replace = array(
'>',
'<',
'\\1',
'',
"//<![CDATA[
".'\1'."
//]]>",

);

$buffer = preg_replace($search, $replace, $buffer);


return $buffer;

} // End sanitize_output()

The way I use it:

<?php
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();

// ... Code ... Several CSS ... JS ..

$output = ob_get_contents();
ob_end_clean();
$output =  sanitize_output($output);
echo $output;