I think that many people know how to minify html using php:

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

I tried this method on Bitrix - but the code is not as not perceived by the CMS. Is there an option to compress html on Bitrix in 1 line?

  • Answered, but it is not clear why to do so. Gzip still better to shake - Mihanik71

2 answers 2

Possible to do through the OnEndBufferContent event

 <? AddEventHandler("main", "OnEndBufferContent", "ChangeMyContent"); function ChangeMyContent(&$content) { $search = array( '/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s' ); $replace = array( '>', '<', '\\1' ); $content = preg_replace($search, $replace, $content); } ?> 

The code is placed in init.php.

  • Thank you for help! - Eva_m
  • There is not a big conflict with the admin panel (the source code is also minified there and because of this some functions do not work) can the code be adapted only for the site itself (without the admin panel)? - Eva_m
  • You can set a condition depending on the SITE_ID or folder that is being accessed - Mihanik71
  • Thanks for the help - Eva_m

So you can html compress

 // Удаляем лишние пробелы и пустые строки в html перед выводом пользователю AddEventHandler("main", "OnEndBufferContent", "ChangeMyContent"); function ChangeMyContent(&$content){ $content = sanitize_output($content); } 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 ); $replace = array( '>', '<', '\\1' ); $blocks = preg_split('/(<\/?pre[^>]*>)/', $buffer, null, PREG_SPLIT_DELIM_CAPTURE); $buffer = ''; foreach($blocks as $i => $block){ if($i % 4 == 2) $buffer .= $block; //break out <pre>...</pre> with \n's else $buffer .= preg_replace($search, $replace, $block); } return $buffer; } 

And also css squeeze and bring to the joy of Page Speed ​​Insights

  AddEventHandler("main", "OnEndBufferContent", "deleteKernelCss"); function deleteKernelCss(&$content) { global $USER, $APPLICATION; if ((is_object($USER) && $USER->IsAuthorized()) || strpos($APPLICATION->GetCurDir(), "/bitrix/") !== false || strpos($APPLICATION->GetCurDir(), "/peredvizhnie-stellazhi/")!== false) return; if ($APPLICATION->GetProperty("save_kernel") == "Y") return; $arPatternsToRemove = Array( '/<link.+?href=".+?kernel_main\/kernel_main\.css\?\d+"[^>]+>/', ); $content = preg_replace($arPatternsToRemove, "", $content); $content = preg_replace("/\n{2,}/", "\n\n", $content); } AddEventHandler("main", "OnEndBufferContent", "includeCssInline"); function includeCssInline(&$content) { global $USER, $APPLICATION; if ((is_object($USER) && $USER->IsAuthorized()) || strpos($APPLICATION->GetCurDir(), "/bitrix/") !== false) return; if ($APPLICATION->GetProperty("save_kernel") == "Y") return; preg_match('/<link.+?href="(\/bitrix\/cache\/css\/' . SITE_ID . '\/' . SITE_TEMPLATE_ID . '\/template_[^"]+)"[^>]+>/', $content, $arMatches); $sFilePath = "https://" . $_SERVER["HTTP_HOST"] . $arMatches[1]; $obCache = new CPHPCache; $life_time = 0 * 60; if ($obCache->InitCache($life_time, $sFilePath, "/")) { $vars = $obCache->GetVars(); $sIncludeCss = $vars["sIncludeCss"]; $sIncludeCssClear = $vars["sIncludeCssClear"]; } else { $sIncludeCss = file_get_contents($sFilePath); $sIncludeCssClear = compressCSS($sIncludeCss); } if (false) { ?><pre style="font-size:10px;line-height: 8px;">До: <?= strlen($sIncludeCss); ?></pre><? ?><pre style="font-size:10px;line-height: 8px;">После: <?= strlen($sIncludeCssClear); ?></pre><? } $content = str_replace($arMatches[0], "<style>$sIncludeCssClear</style>", $content); if ($obCache->StartDataCache()) { $obCache->EndDataCache(array( "sIncludeCss" => $sIncludeCss, "sIncludeCssClear" => $sIncludeCssClear, )); } } function compressCSS($css, $arOptions = Array()) { $sResult = $css; $sResult = preg_replace("/\/\*[^*]+\*\//", "", $sResult); // comments $sResult = preg_replace("/\/\**\*\//", "", $sResult); // comments $sResult = preg_replace("/\s*(:|,|;|{|}|\t)\s*/", "$1", $sResult); // whitespaces $sResult = preg_replace("/(\t+|\s{2,})/", " ", $sResult); // tabs and double whitespace $sResult = preg_replace("/(\s|:)([\-]{0,1}0px)\s/", " 0 ", $sResult); // zeros $sResult = preg_replace("/#(\w){6};/", "#$1$1$1;", $sResult); // #dddddd => #ddd return $sResult; } 

I collected pieces in the open spaces) I do not pretend to authorship