|
Эту функцию можно использовать для уменьшения размера выдаваемой страницы за счет удаления пробелов, переводов строк и комментариев.
Так же есть обработка проблемы с BOM-символом, и когда входная строка склеена из строк в разных кодировках.
<?php
/**
* packHtml returns html without spaces, comments and new line symbols,
* but keeps formatting in conditional comments and
* in these tags: script, textarea, style and special tag <ignore_pack>;
*
* @param type $htmlStr
* @return type
*/
function packHtml( $htmlStr ) {
// save doctype
/*
this regexp '/^.*?(<!doctype.+?>).*?(\n|\r|\r\n)/iu' starts with ^.*? for remove BOM-symbol
*/
if( preg_match( '/^.*?(<!doctype.+?>).*?(\n|\r|\r\n)/iu', $htmlStr, $matches ) ) {
$htmlStr = preg_replace( "/^.*?<!doctype.+?>.*?(\n|\r|\r\n)/iu", '', $htmlStr );
$doctype = $matches[1] . $matches[2];
} else {
$doctype = '';
}//if
// order of removing
// first of all remove and save conditional comments, wich can contain scripts, styles, and tags
// conditional comment starts only with <!--[ without space -->
$pattern = '/<!--\[.*?\]-->/su';
preg_match_all( $pattern, $htmlStr, $condComments );
$condComments = $condComments[0];
// sometimes, result page may be combined with number of template files, wich can be in different
// encodings, and for that case I place this condition
if( count( $condComments ) > 0 ) {
$htmlStr = preg_replace( $pattern, "<%condComments%>", $htmlStr );
}//if
$pattern = '/<ignore_pack>(.*?)<\/ignore_pack>/su';
preg_match_all( $pattern, $htmlStr, $ignore_pack );
$ignore_pack = $ignore_pack[1];
if( count( $ignore_pack ) > 0 ) {
$htmlStr = preg_replace( $pattern, "<%ignore_pack%>", $htmlStr );
}//if
$tags_to_save = array( 'script', 'style', 'textarea' );
foreach( $tags_to_save as $tag ) {
$i = 0;
$pattern = "/<$tag.*?>.*?<\/$tag>/su";
while( preg_match( $pattern, $htmlStr, $match ) ){
$saved_code[$tag][] = $match[0];
$htmlStr = preg_replace( $pattern, "<%{$tag}{$i}%>", $htmlStr, 1 );
$i++;
}//while
}//foreach
$pattern = "/<!--.*?-->/su";
if( preg_match( $pattern, $htmlStr ) ) {
$htmlStr = preg_replace( $pattern, '', $htmlStr );
}//if
// pack html after saving non formating code
$pattern = array("\r\n", "\n", "\r");
$htmlStr = str_replace( $pattern, ' ', $htmlStr );
$pattern = '/\s+/iu';
if( preg_match( $pattern, $htmlStr ) ) {
$htmlStr = preg_replace( $pattern, ' ', $htmlStr );
}//if
// after pack restore saved code
$htmlStr = $doctype . $htmlStr;
$pattern = "<%ignore_pack%>";
while( mb_strpos( $htmlStr, $pattern ) ){
$htmlStr = preg_replace( "/$pattern/u", array_shift( $ignore_pack ), $htmlStr, 1 );
}//while
$pattern = "<%condComments%>";
while( mb_strpos( $htmlStr, $pattern ) ){
$htmlStr = preg_replace( "/$pattern/u", array_shift( $condComments ), $htmlStr, 1 );
}//while
foreach( $tags_to_save as $tag ) {
$i = 0;
while( mb_strpos( $htmlStr, "<%$tag" ) ){
$htmlStr = preg_replace( "/<%{$tag}{$i}%>/u", $saved_code[$tag][$i], $htmlStr, 1 );
$i++;
}//while
}//foreach
return $htmlStr;
}//packHtml
Вернуться к списку записей комментарии работают на Disqus |
В админку |