Is it possible to redirect "print" to a string?

Discussion in 'PHP' started by Glowing Face Man, Jan 25, 2010.

  1. #1
    Hi, I have some code which sends output directly to the user. I'd like to change it to send the output to a string, instead. This includes stuff outside php tags, as well as contents of "print" commands.

    For example, something like this...

    <?php
    $_SOMEGLOBAL['OUTPUT_TARGET'] = $mystring;
    ?>Blah blah <?php print "blah";
    // $mystring should now be "Blah blah blah" and no output has been sent yet
    PHP:
    Is something like this possible? :)
     
    Glowing Face Man, Jan 25, 2010 IP
  2. rockyg

    rockyg Peon

    Messages:
    230
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You mean you want to append 'blah blah blah' onto $mystring?

    Just do

    $mystring=$mystring."<br/>blah blah blah";

    Now you can print $mystring whenever you want - including your 'blah blah blah'
     
    rockyg, Jan 25, 2010 IP
  3. Glowing Face Man

    Glowing Face Man Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks... Thing is, I'm hoping to avoid going through this rather large amount of code and changing all the prints to appends. :p

    (Moral of the story: in general, any time you're generating virtual pages, store all the input in a string from stage 1, and don't output it 'til the end...)
     
    Glowing Face Man, Jan 25, 2010 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ob_start();
    print "<p>Hello</p>";
    print "<p>I am a blue rhinosceros.</p>";
    $all_the_stuff_i_printed = ob_get_clean();
    PHP:
     
    SmallPotatoes, Jan 25, 2010 IP
  5. Glowing Face Man

    Glowing Face Man Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Awesome!! Thank you! :D:eek::)
     
    Glowing Face Man, Jan 26, 2010 IP