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

Posted on 2003-03-12
Medium Priority
Last Modified: 2006-11-17

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
Question by:rhoepperger
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 15

Expert Comment

ID: 8123109
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.

Author Comment

ID: 8123138
Thanks VGR, but I'd rather not change the existing PHP code if possible.

But thanks for your answer.
LVL 15

Expert Comment

ID: 8123249
well well well wanting the Moon without changing your code ? :D

Are you in Marketing ? :/
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

LVL 15

Accepted Solution

VGR earned 200 total points
ID: 8127304
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).


Author Comment

ID: 8127934

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!


LVL 15

Expert Comment

ID: 8128008
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

Author Comment

ID: 8128069
happy that your're happy that I'm happy :))

Thanks again!

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question