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