Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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

Hello,

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.

But

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
Reinhard
0
rhoepperger
Asked:
rhoepperger
  • 4
  • 3
1 Solution
 
VGRCommented:
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.
0
 
rhoeppergerAuthor Commented:
Thanks VGR, but I'd rather not change the existing PHP code if possible.

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

Are you in Marketing ? :/
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
VGRCommented:
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).

http://www.php.net/manual/en/ref.outcontrol.php
0
 
rhoeppergerAuthor Commented:
Perfect!  

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.

Awesome!

Thanks a lot for your help!

Reinhard

0
 
VGRCommented:
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
0
 
rhoeppergerAuthor Commented:
happy that your're happy that I'm happy :))

Thanks again!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now