PHP 出力を圧縮する関数がありますが、インライン JavaScript で問題が発生します。
関連トピックのページを見つけましたが、そこからのサンプルは機能しません: http://jeromejaglale.com/doc/php/codeigniter_compress_html
// 複数の空白シーケンスを短縮すると問題が発生します
この部分を配列から削除すると、Javascript はうまく機能します。
質問:
MinifyやTidyなどの他のライブラリを使用しなくても、目的の結果を達成できる可能性はありますか?
関数:
/**
* [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[\n".'\1'."\n//]]>",
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
} // End sanitize_output()
私がそれを使用する方法:
<?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;