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!
Copyright Notice:Content Author:「Tommaso Belluzzo」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7456534/php-output-buffer-content