redirection of echo() and other functions into a temporary file


I have a PHP problem that might be simple or impossible to
solve, I don't know yet which it is.

Here's the problem:

I have a PHP file that (as usual) generates some HTML-code.
Nothing special with that.


When all is done and generated, I would like to
post-process that HTML-code.  For example I would like
to remove HTML-comments, carriage returns end remove
unnecessary whitespaces to compact the file as much
as possible.

I don't want to overload echo() so that all these
manipulations are done during the write, but rather
I would like to have ONE place (at the end of the
PHP file) where ALL the html-code (that would now
be sent to the server and then to the browser)
can be processed at once.

In other words:
The PHP-function echo() writes HTML-code.
Is there a read-function that can read that HTML-code?

And if not, is there a way to specify at the beginning
of the PHP-file:  "From now on all echo-output is added
to the file xyz rather than to the server".   Is such
redirection of "standard out" to a temporary file
possible in PHP ?

If it was then I could simply
a)  At the top of the file say "Redirect output to xyz"
b)  At the end of the file say "close xyz, open it
    for input and read it in while you manipulate the
    code into the final html-code).

Thanks a LOT for your help with this one!

Warm greetings
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

unless you find a way to actually redirect output (stdout, OutPut, etc) to a file, I suggest to try to use the ob_* functions (output buffering). May-be you'll find your way there.
rhoeppergerAuthor Commented:
Thanks VGR, but I'd rather not change the existing PHP code if possible.

But thanks for your answer.
well well well wanting the Moon without changing your code ? :D

Are you in Marketing ? :/
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

some answer found in the PHP doc :

Tip: As with anything that outputs its result directly to the browser, you can use the output-control functions to capture the output of this function, and save it in a string (for example).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rhoeppergerAuthor Commented:

Thanks a lot VGR!  That is EXACTLY what I was looking for.

Now I place a
ob_start() at the top of the file and a
$ret_str = ob_get_contents(); ob_end_clean(); at the end

and can now process my whole output in the string.


Thanks a lot for your help!


hence "I suggest to try to use the ob_* functions (output buffering)" I told you about in the first instance 8-))

happy that you're happy
rhoeppergerAuthor Commented:
happy that your're happy that I'm happy :))

Thanks again!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.