How to process a local file rather than just read it

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ashepster@gmail.com

    How to process a local file rather than just read it

    I can read a local file with

    fopen("myFile.p hp");

    However this gives me the source of the file (i.e. php code). I want
    to get the results of the file after it has been evaluated (i.e. HTML).


    The obvious way of doing this is

    include 'myfile.php';

    However, I want to process the evaluated result. For example:

    echo htmlentities( include 'myfile.php' );

    This doesn't work because the result of the include statement is true
    or false rather than a string.

    I'm sure this is very simple, but I'm just learning.

    - Jeff -

  • ZeldorBlat

    #2
    Re: How to process a local file rather than just read it

    Use include() to parse the file and capture the output with output
    buffering. For instance:

    ob_start();
    include 'myfile.php';
    $output = ob_get_contents ();
    ob_end_clean(); //discard the buffer since we already have the contents

    echo htmlentities($o utput);

    Comment

    • ashepster@gmail.com

      #3
      Re: How to process a local file rather than just read it

      Wow. That was entirely non-intuitive. I would have thought there was
      some php-evaluate() function I was overlooking.

      Works great, though. Thanks!

      - Jeff -

      Comment

      • NC

        #4
        Re: How to process a local file rather than just read it

        ashepster@gmail .com wrote:[color=blue]
        >
        > Wow. That was entirely non-intuitive. I would have thought
        > there was some php-evaluate() function I was overlooking.[/color]

        There is: http://www.php.net/eval

        But include() works much faster.

        Cheers,
        NC

        Comment

        • BKDotCom

          #5
          Re: How to process a local file rather than just read it

          I suppose/think you could load it with fopen etc as you describe and
          then use eval($file_cont ents)... but that would be silly

          Comment

          • Iván Sánchez Ortega

            #6
            Re: How to process a local file rather than just read it

            -----BEGIN PGP SIGNED MESSAGE-----
            Hash: SHA1

            ashepster@gmail .com wrote:
            [color=blue]
            > I would have thought there was
            > some php-evaluate() function I was overlooking.[/color]

            You can do it that way too. Have a look at http://php.net/eval .

            - --
            - ----------------------------------
            Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

            Un ordenador no es un televisor ni un microondas, es una herramienta
            compleja.
            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.4.2 (GNU/Linux)

            iD8DBQFDYoNx3jc Q2mg3Pc8RAlLlAJ 9tO8uc8XcuYedfQ +EBa4ZqlZ9fZACf f++6
            jEEaFzMStjD6FJd yaBsOja8=
            =4Or1
            -----END PGP SIGNATURE-----

            Comment

            Working...