Read *parsed* PHP file into a variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Loose

    #1

    Read *parsed* PHP file into a variable?

    Hello,

    I have this strange including problem:

    I want to read a piece of HTML, residing in some file, into a
    variable, not echo it out. So far so easy - but now the HTML contains
    a line of PHP. And I wish not to get the PHP code into my variable but
    the parsed result, just like if the variable was a client ;-). I wish
    to keep the nice ?> ...<? HTML area (with highlighting in my editor),
    and therefore include etc. don't help because they would echo it out.
    heredoc also doesnt help (if this may come up to your mind).

    Is there anything I can do...?!! The problem sounds so simple... the
    more for such a great tool like PHP ... but... ?!!

    Thanx a lot,
    Daniel

    PS Please post also if you know for sure that there is NO solution to
    my problem

  • Philip Ronan

    #2
    Re: Read *parsed* PHP file into a variable?

    "Daniel Loose" wrote:
    [color=blue]
    > Hello,
    >
    > I have this strange including problem:
    >
    > I want to read a piece of HTML, residing in some file, into a
    > variable, not echo it out. So far so easy - but now the HTML contains
    > a line of PHP. And I wish not to get the PHP code into my variable but
    > the parsed result, just like if the variable was a client ;-). I wish
    > to keep the nice ?> ...<? HTML area (with highlighting in my editor),
    > and therefore include etc. don't help because they would echo it out.
    > heredoc also doesnt help (if this may come up to your mind).
    >
    > Is there anything I can do...?!! The problem sounds so simple... the
    > more for such a great tool like PHP ... but... ?!![/color]

    Easy: $html = file_get_conten ts('http://www.example.com/foo.bar');

    Take a look here: <http://php.net/file_get_conten ts>

    --
    phil [dot] ronan @ virgin [dot] net



    Comment

    • Kimmo Laine

      #3
      Re: Read *parsed* PHP file into a variable?

      "Daniel Loose" <noreply@web.de > kirjoitti
      viestissä:430cb 793.9399600@new s.cs.tu-berlin.de...[color=blue]
      > Hello,
      >
      > I have this strange including problem:
      >
      > I want to read a piece of HTML, residing in some file, into a
      > variable, not echo it out. So far so easy - but now the HTML contains
      > a line of PHP. And I wish not to get the PHP code into my variable but
      > the parsed result, just like if the variable was a client ;-). I wish
      > to keep the nice ?> ...<? HTML area (with highlighting in my editor),
      > and therefore include etc. don't help because they would echo it out.
      > heredoc also doesnt help (if this may come up to your mind).
      >
      > Is there anything I can do...?!! The problem sounds so simple... the
      > more for such a great tool like PHP ... but... ?!![/color]


      Sounds like you could use buffering.
      <?php

      ob_start(); // Start output buffering.
      // Instead of outputting anything, the output is now stored in a buffer.
      include('yourfi le.php');
      // There, now the file has been included and php parsed.
      // It isn't output, it goes into the buffer.
      $yourfile = ob_get_clean();
      // Now we read everything that has been buffered... And stops buffering.

      ?>

      Result: the file you included has been parsed by the php engine and stored
      inside $yourfil, and nothing was output. Sounds like goal achieved.

      --
      SETI @ Home - Donate your cpu's idle time to science.
      Further reading at <http://setiweb.ssl.ber keley.edu/>
      Kimmo Laine <eternal.erecti onN0@5P4Mgmail. com>


      Comment

      • Daniel Loose

        #4
        Re: Read *parsed* PHP file into a variable?

        Just discovered http://de.php.net/eval and on that url especially the
        user contributed function eval_html3(). But can't get it to work.

        I made a simple test page:

        Perhaps you feel like helping to fix the function. (Or to tell me what
        I have misunderstood.) I just renamed eval_html3 to eval_html and
        replaced <?php by <?. It's now supposed to work but it doesnt. Here
        the test files:

        test.php:
        ------------
        $s = eval_html(file_ get_contents('. js.php', 0));
        $t = eval_html(file_ get_contents('. css.php', 0));

        function eval_html($stri ng) {
        $string = '<? ?>'.$string.'< ? ?>';
        $string = str_replace( '?>', '', str_replace( array( '<?', '<?' ),
        '', preg_replace_ca llback( "/\?>(.*?)(<\?|<\ ?)/", "my_eval", $string )
        ) );
        return eval($string);
        }
        function my_eval($arr) {
        return ('echo stripslashes("' .addslashes($ar r[0]).'");');
        }

        ..js.php and .css.php:
        ------------------------------
        contain plain js/ css plus one line
        <? $a = 6+7; echo ".testClass { font-size: ".$a."px; }\n\n"; ?>
        or resp.
        <? $a = 4+5; echo 'testVar = '.$a;; ?>
        No errors when called directly.

        test.php itself does nothing (blank screen), I only want to get away
        the errors. You may also download this small test if you want to
        figure it out on your own system (See Link)

        Thanx a lot! , Daniel

        PS Thx to the other poster, will check your idea out soon as possible.

        Comment

        • Janwillem Borleffs

          #5
          Re: Read *parsed* PHP file into a variable?

          Daniel Loose wrote:[color=blue]
          > Just discovered http://de.php.net/eval and on that url especially the
          > user contributed function eval_html3(). But can't get it to work.
          >[/color]

          Better have a look at Kimmo's suggestion, because that's the way to go...


          JW



          Comment

          • Daniel Loose

            #6
            Re: Read *parsed* PHP file into a variable?

            [color=blue]
            >Sounds like you could use buffering.
            ><?php
            >
            >ob_start(); // Start output buffering.
            >// Instead of outputting anything, the output is now stored in a buffer.
            >include('yourf ile.php');
            >// There, now the file has been included and php parsed.
            >// It isn't output, it goes into the buffer.
            >$yourfile = ob_get_clean();
            >// Now we read everything that has been buffered... And stops buffering.
            >[/color]

            Pretty cool, thanx so much! Never heard of output buffering before.
            Yeah...

            Guess no need to fight with my other Reply anymore (concerning
            eval_html()).

            Enjoy, D.

            Comment

            • R. Rajesh Jeba Anbiah

              #7
              Re: Read *parsed* PHP file into a variable?

              Daniel Loose wrote:[color=blue]
              > Hello,
              >
              > I have this strange including problem:
              >
              > I want to read a piece of HTML, residing in some file, into a
              > variable, not echo it out. So far so easy - but now the HTML contains
              > a line of PHP. And I wish not to get the PHP code into my variable but
              > the parsed result, just like if the variable was a client ;-). I wish
              > to keep the nice ?> ...<? HTML area (with highlighting in my editor),
              > and therefore include etc. don't help because they would echo it out.
              > heredoc also doesnt help (if this may come up to your mind).[/color]
              <snip>

              Sounds like you're going to use/eval users' input?--if so, it's a
              *serious* security issue. Use output buffering with runkit
              <http://in.php.net/runkit>

              --
              <?php echo 'Just another PHP saint'; ?>
              Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

              Comment

              Working...