简体   繁体   中英

How to minify only specified file/part of page using PHP

How I can use this function to skip specified files and not minify them?

Minifier:

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');

Text here. Text here.

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

If you need minify next code -- call ob_start() manually before code block:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM