我该怎么做?
考虑使用以下链接来压缩Javascript/CSS文件:https://github.com/mrclay/minify
告诉Apache使用GZip发送HTML-这通常可以将响应大小减少约70%。 (如果您使用Apache,则配置gzip的模块取决于您的版本:Apache 1.3使用mod_gzip,而Apache 2.x使用mod_deflate。)
Accept-Encoding: gzip, deflate
Content-Encoding: gzip
使用以下代码片段结合ob_start的缓冲区来删除HTML中的空白:
<?php
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
'/<!--(.|\s)*?-->/' // Remove HTML comments
);
$replace = array(
'>',
'<',
'\\1',
''
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
ob_start("sanitize_output");
?>
$content = \Minify_HTML::minify($content);
),它甚至可以为行内代码添加回调到js/css缩小器。请参见 https://github.com/mrclay/minify/blob/master/min/lib/Minify/HTML.php - Barryvdh<script>
标签中的JavaScript),如果每个语句末尾没有;
或者有使用//
的注释。 - Konstantin Pereiaslovfunction minify_html($html)
{
$search = array(
'/(\n|^)(\x20+|\t)/',
'/(\n|^)\/\/(.*?)(\n|$)/',
'/\n/',
'/\<\!--.*?-->/',
'/(\x20+|\t)/', # Delete multispace (Without \n)
'/\>\s+\</', # strip whitespaces between tags
'/(\"|\')\s+\>/', # strip whitespaces between quotation ("') and end tags
'/=\s+(\"|\')/'); # strip whitespaces between = "'
$replace = array(
"\n",
"\n",
" ",
"",
" ",
"><",
"$1>",
"=$1");
$html = preg_replace($search,$replace,$html);
return $html;
}
如果你想正确地使用,可以打开gzip。你也可以像这样做:
$this->output = preg_replace(
array(
'/ {2,}/',
'/<!--.*?-->|\t|(?:\r?\n[ \t]*)+/s'
),
array(
' ',
''
),
$this->output
);
通过将您的HTML转换为一行,无制表符、无换行符和无注释,可以减少约30%的页面大小。具体效果因情况而异。
我尝试了几个压缩工具,但它们要么移除太少,要么移除太多。
这段代码可以去除冗余的空格和可选的HTML(结束)标签。同时它保证不会移除任何可能破坏HTML、JS或CSS的内容。
此外,该代码还展示了在Zend框架中如何实现这一功能:
class Application_Plugin_Minify extends Zend_Controller_Plugin_Abstract {
public function dispatchLoopShutdown() {
$response = $this->getResponse();
$body = $response->getBody(); //actually returns both HEAD and BODY
//remove redundant (white-space) characters
$replace = array(
//remove tabs before and after HTML tags
'/\>[^\S ]+/s' => '>',
'/[^\S ]+\</s' => '<',
//shorten multiple whitespace sequences; keep new-line characters because they matter in JS!!!
'/([\t ])+/s' => ' ',
//remove leading and trailing spaces
'/^([\t ])+/m' => '',
'/([\t ])+$/m' => '',
// remove JS line comments (simple only); do NOT remove lines containing URL (e.g. 'src="http://server.com/"')!!!
'~//[a-zA-Z0-9 ]+$~m' => '',
//remove empty lines (sequence of line-end and white-space characters)
'/[\r\n]+([\t ]?[\r\n]+)+/s' => "\n",
//remove empty lines (between HTML tags); cannot remove just any line-end characters because in inline JS they can matter!
'/\>[\r\n\t ]+\</s' => '><',
//remove "empty" lines containing only JS's block end character; join with next line (e.g. "}\n}\n</script>" --> "}}</script>"
'/}[\r\n\t ]+/s' => '}',
'/}[\r\n\t ]+,[\r\n\t ]+/s' => '},',
//remove new-line after JS's function or condition start; join with next line
'/\)[\r\n\t ]?{[\r\n\t ]+/s' => '){',
'/,[\r\n\t ]?{[\r\n\t ]+/s' => ',{',
//remove new-line after JS's line end (only most obvious and safe cases)
'/\),[\r\n\t ]+/s' => '),',
//remove quotes from HTML attributes that does not contain spaces; keep quotes around URLs!
'~([\r\n\t ])?([a-zA-Z0-9]+)="([a-zA-Z0-9_/\\-]+)"([\r\n\t ])?~s' => '$1$2=$3$4', //$1 and $4 insert first white-space character found before/after attribute
);
$body = preg_replace(array_keys($replace), array_values($replace), $body);
//remove optional ending tags (see http://www.w3.org/TR/html5/syntax.html#syntax-tag-omission )
$remove = array(
'</option>', '</li>', '</dt>', '</dd>', '</tr>', '</th>', '</td>'
);
$body = str_ireplace($remove, '', $body);
$response->setBody($body);
}
}
但需要注意的是使用gZip压缩后,你的代码被压缩的程度比任何代码缩小技术都要多,所以结合代码缩小和gZip是没有意义的,因为下载节省的时间会因为代码缩小而损失,同时节省的效果也很有限。
以下是我的测试结果(通过3G网络下载):
Original HTML: 150kB 180ms download
gZipped HTML: 24kB 40ms
minified HTML: 120kB 150ms download + 150ms minification
min+gzip HTML: 22kB 30ms download + 150ms minification
),
存在问题,例如 (CEO), 2015
。倒数第二个正则表达式(“删除JS行末的换行符...”)会错误地删除它们之间的空格。 - Ti Hausmann之前所有的preg_replace()
解决方案都存在单行注释、条件注释和其他问题。我建议利用经过良好测试的Minify项目而不是自己从头开始创建正则表达式。
在我的情况下,我将以下代码放置在PHP页面的顶部来对其进行缩小:
function sanitize_output($buffer) {
require_once('min/lib/Minify/HTML.php');
require_once('min/lib/Minify/CSS.php');
require_once('min/lib/JSMin.php');
$buffer = Minify_HTML::minify($buffer, array(
'cssMinifier' => array('Minify_CSS', 'minify'),
'jsMinifier' => array('JSMin', 'minify')
));
return $buffer;
}
ob_start('sanitize_output');
Create a PHP file outside your document root. If your document root is
/var/www/html/
create the a file named minify.php one level above it
/var/www/minify.php
Copy paste the following PHP code into it
<?php function minify_output($buffer){ $search = array('/\>[^\S ]+/s','/[^\S ]+\</s','/(\s)+/s'); $replace = array('>','<','\\1'); if (preg_match("/\<html/i",$buffer) == 1 && preg_match("/\<\/html\>/i",$buffer) == 1) { $buffer = preg_replace($search, $replace, $buffer); } return $buffer; } ob_start("minify_output");?>
Save the minify.php file and open the php.ini file. If it is a dedicated server/VPS search for the following option, on shared hosting with custom php.ini add it.
auto_prepend_file = /var/www/minify.php
参考资料:http://websistent.com/how-to-use-php-to-minify-html-output/
本文介绍如何使用PHP来压缩HTML输出内容。通过减少代码的大小,可以提高网站的加载速度并节省带宽。在这篇文章中,我们将讨论两种方法:手动压缩和使用第三方库。ob_start('ob_gzhandler');
phpinfo()
... 至少应该安装了 zlib
,这样您就可以使用 ob_gzhandler
。 - Rudi Visserif (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
,这不是一样的吗? - m3tsyselse ob_start()
部分,也不需要 gzip 检查... ob_gzhandler
内部检测浏览器是否支持任何压缩方法。只需使用 ob_start('ob_gzhandler');
就足够了。 - Rudi Visserob_start(function($b){
if(strpos($b, "<html")!==false) {
return str_replace(PHP_EOL,"",$b);
} else {return $b;}
});
PHP_EOL
比我之前使用的array("\r\n","\n", "\r")
更好,因为它不会从文本区域的内容中删除换行符,而PHP_EOL
则不会。谢谢! - Moseleyi<?php
include 'path/to/php-html-css-js-minifier.php';
ob_start('minify_html');
?>
<!-- HTML code goes here ... -->
<?php echo ob_get_clean(); ?>
ob_start(function($b){return preg_replace(['/\>[^\S ]+/s','/[^\S ]+\</s','/(\s)+/s'],['>','<','\\1'],$b);});
意为使用ob_start()
函数和一个匿名函数作为参数,在输出前对文本进行正则表达式替换,将多余的空格和换行符去除,使文本更加紧凑。 - Francisco Presencia<pre>
或<code>
标签,因为它们需要空格来构建正确的结构。但是,<script>
应该是外部的通常情况下,或者内联但使用严格的;
方式,以便它也能工作。还可能会破坏哪些其他标签@Brad?我想不到其他的了。在我的先前评论之前,我应该添加“快速而粗略的方式”。 - Francisco Presencia