如何使用PHP缩小指定的文件/页面的一部分

时间:2018-08-29 19:32:40

标签: php minify ob-start

我如何使用此功能跳过指定的文件而不缩小文件?

缩小器:

function sanitize_output($buffer) {
  $search = array(
    '/\>[^\S ]+/s',
    '/[^\S ]+\</s',
    '/(\s)+/s',
    '/<!--(.|\s)*?-->/'
  );
  $replace = array(
    '>',
    '<',
    '\\1',
    ''
  );
  $buffer = preg_replace($search, $replace, $buffer);
  return $buffer;
}
ob_start("sanitize_output");

HTML:

// this file contains above minifier
include($_SERVER['DOCUMENT_ROOT'].'/include-minifier.php');


// should be minified
<p style='display:      block'>custom text</p>

// do not minify this file
include($_SERVER['DOCUMENT_ROOT'].'/body.php');

在此处输入文字。在此处输入文字。

1 个答案:

答案 0 :(得分:0)

// do not minify this file
ob_flush();
ob_end_clean();
include($_SERVER['DOCUMENT_ROOT'].'/body.php');

如果您需要精简下一个代码,请在代码块之前手动调用ob_start()

<?php ob_start("sanitize_output"); ?>
...any html...