PHP Output Buffer Content
NickName:Tommaso Belluzzo Ask DateTime:2011-09-18T01:23:36

PHP Output Buffer Content

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:

  1. It will be a real nightmare to modify my CSS after, if needed.
  2. 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

Answers
johannes 2011-09-17T17:28:21

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.\n\nAs 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.",


More about “PHP Output Buffer Content” related questions

PHP Output Buffer Content

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 Bu...

Show Detail

Browser Caching dynamic content using PHP output buffer and ETag

I have been researching some strategies to optimize a web application I am working on particularly related to web browser caching and dynamic data. Since potentially the same dynamic content may be

Show Detail

PHP: Output buffer callback not altering output

I have an output buffer with callback function. When cleaning the buffer the callback function is executed, however, the string returned isn't being altered. I'm using following code: &lt;?php ob...

Show Detail

PHP URL Rewriting from Output Buffer

I am trying to rewrite my URLs in PHP using the output buffer. Ideally this is so in my source I can keep my links as example.com/index.php?action=123;sa=456;sa2=789 but when outputted to the brows...

Show Detail

PHP Output buffer

My own PHP framework parses files like this to grab the content: ob_start(); include($file); $content = ob_get_clean(); However, now I am working on an own error_handler to display an error page ...

Show Detail

Write to PHP output buffer and then download CSV from buffer

I need to write a CSV file to the PHP output buffer and then download that file to the client's computer after it's done writing. (I wanted to just write it on the server and download it which was

Show Detail

Problems with PHP Output Buffer Flush

I have a PHP script which takes a while to process and I wanted to get the site to display it's progress while it is running. I have been having problems displaying the contents of the output buffer

Show Detail

How to see php error in included file while output buffer?

Blank screen when using output buffer and there's syntax errors in included file. PHP doesn't show errors from output buffer. How to see php output buffer syntax errors? In my project I used @ for

Show Detail

PHP output buffer is not empty?

I have this peculiar problem. I am making an AJAX call to a PHP page. In case of error I am returning the string "error" to success: function(msg) i.e. msg will have the value "error". But for some

Show Detail

Pausing output buffer in php

Here's my problem. I want to be able to buffer only the contents of the tables but not the header. Is it possible to pause the output buffering in php so that I could skip the buffering on the table

Show Detail