PHP 输出缓冲区内容
我使用 PHP 创建基于用户代理的动态样式表:
AddHandler application/x-httpd-php .css
我使用 gzip(基于 php.ini)将它们发送给客户端:
output_handler = ob_gzhandler
但我也想缩小动态样式表的内容以获得更好的性能。因此,在样式表的末尾,我放置了:
input.confirmation
{
<?php if ($Browser == 'lt8') { ?>
margin-top: 1px;
<?php } else { ?>
margin-top: 3px;
<?php } ?>
}
<?php echo Minify(ob_get_clean()); ?>
其中“function Minify($CSSCode)”仅返回我放入参数中的字符串的缩小版本。问题是这只是输出一个空的样式表。我还尝试了以下代码:
<?php
$Content = ob_get_contents();
ob_clean();
echo Minify($Content);
?>
但我得到了相同的结果:空文件。如果我使用:
<?php echo Minify(ob_get_contents()); ?>
我的 shylesheet 将包含未缩小和缩小的代码。 我想到的一个解决方案是将变量中的每个样式表行连接起来,最后像这样打印它:
$CSSCode = '';
[...]
$CSSCode .= "#header";
$CSSCode .= "{";
$CSSCode .= " display: block;";
$CSSCode .= " height: 100px;";
$CSSCode .= "}";
[...]
echo Minify($CSSCode);
但我更愿意避免这种做法,因为:
- 如果需要的话,之后修改我的CSS将是一场真正的噩梦。
- 我的整个网站只有一个样式表,而且它很长......因此将其转换为基于变量的样式表可能会非常耗时。
如何正确清除和覆盖输出缓冲区?
提前致谢!
I'm using PHP to create user-agent based dynamic stylesheets with:
AddHandler application/x-httpd-php .css
And I send them to client using gzip (php.ini based):
output_handler = ob_gzhandler
But I also want to minify the content of my dynamic stylesheets in order to get better performances... so, at the end of my stylesheet I put:
input.confirmation
{
<?php if ($Browser == 'lt8') { ?>
margin-top: 1px;
<?php } else { ?>
margin-top: 3px;
<?php } ?>
}
<?php echo Minify(ob_get_clean()); ?>
Where "function Minify($CSSCode)" just returns a minified version of the string I put in the argument. The problem is that this just outputs an empty stylesheet. I also tried the following code:
<?php
$Content = ob_get_contents();
ob_clean();
echo Minify($Content);
?>
But I obtain the same result: empty file. If I use instead:
<?php echo Minify(ob_get_contents()); ?>
My shylesheet will contain both unminified and minified code.
A solution I thought about is to concatenate every single stylesheet line inside a variable and print it in the end like this:
$CSSCode = '';
[...]
$CSSCode .= "#header";
$CSSCode .= "{";
$CSSCode .= " display: block;";
$CSSCode .= " height: 100px;";
$CSSCode .= "}";
[...]
echo Minify($CSSCode);
But I would prefer to avoid this practice because:
- It will be a real nightmare to modify my CSS after, if needed.
- I have only one stylesheet for my whole website and it's quite long... so transforming it into a variable based stylesheet risks to be really really time expensive.
How can I properly clear and override the output buffer?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以做的是在脚本开头再次执行显式
ob_start()
,这样您就可以获得第二个缓冲区,因为它们可以嵌套。一般来说,压缩应该很好地处理空白,因此 Minify 操作的增益最终应该几乎不显着。备注:当从脚本生成 CSS 等时,请确保设置正确的缓存过期标头,以便客户端不会每次都请求 CSS 文件,而是缓存它们。这比你能做的任何其他事情带来的收获都要大得多。
What you can do is to do an explicit
ob_start()
again in the beginning of the script so you get a second buffer as they can be nested. In general the compression should take good care of the whitespaces so the gain of the Minify operation should be barely notable in the end.As a remark: When generating CSS etc. from a script make sure to set proper cache expiration headers so the client won't request the CSS files everytime but caches them. This brings a way bigger gain than any of the other things you can do.
在
ob_start
之后,您必须ob_end_clean
(或使用其变体之一),否则输出缓冲仍处于开启状态,并且不会输出任何内容。 =)http://php.net/manual/en/function.ob -end-clean.php
after
ob_start
, you mustob_end_clean
(or use one of its variants), or else the Output Buffering is still ON, and no content will be outputted. =)http://php.net/manual/en/function.ob-end-clean.php