using echo but need something to execute php code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Slaughter

    #1

    using echo but need something to execute php code

    I have some code that loads up some php/html files and does a few things to
    them and ultimately returns an html file with some php code in it. I then
    pass that file onto the user by using echo. Of course then the file doesn't
    get seen by the user.

    Is there any command that essentially executes the code and then echo's it?

    something that will take a string like

    '<body>blah<?ph p echo 'Hello'; ?></body>' and actually interpret the php
    code?

    e.g., doing

    echo '<body>blah<?ph p echo 'Hello'; ?></body>';

    does me no good caues the php code isn't interpreted.

    Saving it to a file and then redirecting the user to that file so it will be
    executed seems like a roundabout way and causes more problems.


    essentially I am trying to include php/html files in one another but I need
    to do some parsing on them first. I'd rather not do something like


    file_get_conten ts($Page)
    parse($Page)
    save($Page)
    include "$Page"


    as it seems uncessary to have to write the page to disk just to include it.

    At




    "Another way to "include" a PHP file into a variable is to capture the
    output by using the Output Control Functions with include(). For example:"

    Seems like I might be able to use that to replace when I use
    file_get_conten ts to get the php code to execute?



    Thanks,
    Jon



  • Janwillem Borleffs

    #2
    Re: using echo but need something to execute php code

    Jon Slaughter wrote:
    Is there any command that essentially executes the code and then
    echo's it?
    For this specific case you could use eval:

    $code = "?><body>blah<? php echo 'Hello'; ?></body>";
    eval($code);

    Note that the closing tag at the beginning of the eval string is mandatory.


    JW


    Comment

    • Jon Slaughter

      #3
      Re: using echo but need something to execute php code


      "Janwillem Borleffs" <jw@jwscripts.c omwrote in message
      news:4633bec1$0 $31213$dbd43001 @news.euronet.n l...
      Jon Slaughter wrote:
      >Is there any command that essentially executes the code and then
      >echo's it?
      >
      For this specific case you could use eval:
      >
      $code = "?><body>blah<? php echo 'Hello'; ?></body>";
      eval($code);
      >
      Note that the closing tag at the beginning of the eval string is
      mandatory.
      >
      >
      Ok. Thats not the specific case I need but maybe it will still work? It is
      essentially what I have. That is, I have an html file with some php code in
      it similar to the example but just more complex. (actually right now its
      just plain html but when I want to add some php stuff I'll need to be able
      to have it evaluated).

      Thanks,
      Jon


      Comment

      • Jeff North

        #4
        Re: using echo but need something to execute php code

        On Sat, 28 Apr 2007 22:11:45 GMT, in comp.lang.php "Jon Slaughter"
        <Jon_Slaughter@ Hotmail.com>
        <BEPYh.1450$RX. 384@newssvr11.n ews.prodigy.net wrote:
        >|
        >| "Janwillem Borleffs" <jw@jwscripts.c omwrote in message
        >| news:4633bec1$0 $31213$dbd43001 @news.euronet.n l...
        >| Jon Slaughter wrote:
        >| >Is there any command that essentially executes the code and then
        >| >echo's it?
        >| >
        >| For this specific case you could use eval:
        >| >
        >| $code = "?><body>blah<? php echo 'Hello'; ?></body>";
        >| eval($code);
        >| >
        >| Note that the closing tag at the beginning of the eval string is
        >| mandatory.
        >| >
        >| >
        >|
        >| Ok. Thats not the specific case I need but maybe it will still work? It is
        >| essentially what I have. That is, I have an html file with some php code in
        >| it similar to the example but just more complex. (actually right now its
        >| just plain html but when I want to add some php stuff I'll need to be able
        >| to have it evaluated).
        Make sure the file has the extension of php.
        ---------------------------------------------------------------
        jnorthau@yourpa ntsyahoo.com.au : Remove your pants to reply
        ---------------------------------------------------------------

        Comment

        • Toby A Inkster

          #5
          Re: using echo but need something to execute php code

          Jon Slaughter wrote:
          That is, I have an html file with some php code in it similar to the
          example but just more complex.
          Let me get this straight... you have an HTML+PHP file. You want your
          script to open this file, run the PHP code inside and echo the whole thing
          out to the client?

          include 'file.php';

          --
          Toby A Inkster BSc (Hons) ARCS
          Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

          Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

          * = I'm getting there!

          Comment

          • Jon Slaughter

            #6
            Re: using echo but need something to execute php code


            "Toby A Inkster" <usenet200703@t obyinkster.co.u kwrote in message
            news:d50eg4-q7c.ln1@ophelia .g5n.co.uk...
            Jon Slaughter wrote:
            >
            >That is, I have an html file with some php code in it similar to the
            >example but just more complex.
            >
            Let me get this straight... you have an HTML+PHP file. You want your
            script to open this file, run the PHP code inside and echo the whole thing
            out to the client?
            >
            include 'file.php';
            You obviously didn't read what I said... Sometimes simple answers are not
            the correct ones.


            Comment

            • Geoff Berrow

              #7
              Re: using echo but need something to execute php code

              Message-ID: <UqZYh.2848$HX7 .607@newssvr19. news.prodigy.ne tfrom Jon
              Slaughter contained the following:
              >include 'file.php';
              >
              >You obviously didn't read what I said... Sometimes simple answers are not
              >the correct ones.

              The answer was correct. It's the question we are having a problem with.
              --
              Geoff Berrow (put thecat out to email)
              It's only Usenet, no one dies.
              My opinions, not the committee's, mine.
              Simple RFDs http://www.ckdog.co.uk/rfdmaker/

              Comment

              • Jon Slaughter

                #8
                Re: using echo but need something to execute php code


                "Geoff Berrow" <blthecat@ckdog .co.ukwrote in message
                news:05s83311je mllsbmo9ur6rkrm l4laptdip@4ax.c om...
                Message-ID: <UqZYh.2848$HX7 .607@newssvr19. news.prodigy.ne tfrom Jon
                Slaughter contained the following:
                >
                >>include 'file.php';
                >>
                >>You obviously didn't read what I said... Sometimes simple answers are not
                >>the correct ones.
                >
                >
                The answer was correct. It's the question we are having a problem with.
                >
                Thats always the case with you guys. Your always right regardless of the
                question. "Whats 2 + 2" -"The milk is made from cheese". "But your
                wrong!" -"No, your question is wrong".

                Your a dipshit...



                Comment

                • Geoff Berrow

                  #9
                  Re: using echo but need something to execute php code

                  Message-ID: <5C_Yh.1820$tp5 .373@newssvr23. news.prodigy.ne tfrom Jon
                  Slaughter contained the following:
                  >Your a dipshit...
                  No, that's /you're/ a dipshit.




                  --
                  Geoff Berrow (put thecat out to email)
                  It's only Usenet, no one dies.
                  My opinions, not the committee's, mine.
                  Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                  Comment

                  • Vince Morgan

                    #10
                    Re: using echo but need something to execute php code

                    "Jon Slaughter" <Jon_Slaughter@ Hotmail.comwrot e in message
                    news:tPOYh.1739 $tp5.630@newssv r23.news.prodig y.net...
                    I have some code that loads up some php/html files and does a few things
                    to
                    them and ultimately returns an html file with some php code in it. I then
                    pass that file onto the user by using echo. Of course then the file
                    doesn't
                    get seen by the user.
                    >
                    If you have an html file, that is, a file containing html markup with "html"
                    or "htm" extension it is _not_ parsed by the php engine,, normaly.
                    Is there any command that essentially executes the code and then echo's
                    it?
                    >
                    Yes, it's called "print" and it can do more complex things than echo will,
                    it's simlar, but not actualy the same.
                    It's described
                    here->http://www.faqts.com/knowledge_base/...l/aid/1/fid/40
                    something that will take a string like
                    >
                    '<body>blah<?ph p echo 'Hello'; ?></body>' and actually interpret the php
                    code?
                    Yes, there is.
                    -------------------------------------
                    !DOCTYPE blah blah blah
                    <html>
                    <head><title> </title></head>
                    <p>stuff in this file that contains html markup with a ".php" extension</p>
                    <p>Other stuff</p>
                    <?php
                    echo '<p>Parse this and include it into the html stuff above and
                    below</p>';
                    ?>
                    <p>Other stuff in this file that has a ".php" extension and will be output
                    as html by the php engine.</p>
                    <?php
                    echo '<p>Parsed and included as html in the output with the other markup
                    around it</p>';
                    ?>
                    <p>More html markup</p>
                    <?php
                    include(myFileI DidStuffTooAMom entAgo.inc);
                    ?>
                    </body>
                    </html>
                    ------------------------------------fileName.php
                    >
                    e.g., doing
                    >
                    echo '<body>blah<?ph p echo 'Hello'; ?></body>';
                    >
                    does me no good caues the php code isn't interpreted.
                    But would if you wrote it as below and saved the file with a php extension..
                    <body>blah
                    <?php
                    echo 'Hello';
                    ?>
                    </body>
                    >
                    Saving it to a file and then redirecting the user to that file so it will
                    be
                    executed seems like a roundabout way and causes more problems.
                    >
                    I would have to agree.
                    >
                    essentially I am trying to include php/html files in one another but I
                    need
                    to do some parsing on them first. I'd rather not do something like
                    Parsing is a very generic term and may include opperations that have nothing
                    to do with html output, or php for that matter.
                    It can only be assumed that you mean "interprete d by the php engine" but you
                    seem to indicate something else by the context here.
                    >
                    >
                    file_get_conten ts($Page)
                    parse($Page)
                    save($Page)
                    include "$Page"
                    >
                    The above could be written into a file with a .php extension as;
                    ---------------------
                    <?php
                    $Page = file_get_conten ts('somepath/Page.txt')
                    function parse(&$Page)
                    {
                    //Losts of parsing code here
                    }
                    parse($Page);
                    file_put_conten ts($Page);
                    ?>
                    ----------------------
                    >
                    as it seems uncessary to have to write the page to disk just to include
                    it.
                    >
                    I would have to agree. So you might write the above as follows;
                    ---------------------
                    <?php
                    $Page = file_get_conten ts('somepath/Page.txt')
                    function parse(&$Page)
                    {
                    //Losts of parsing code
                    }
                    parse($Page);
                    echo $Page;
                    [Or
                    print parse($Page);
                    Or
                    echo parse($Page);
                    ]
                    ?>
                    ----------------------

                    I'm not sure that the above is usefull. There seems to be some confusion
                    about what exactly you are wanting to do.
                    It may be an inaquacey on my part, or it may be ambiguity on your part ;)
                    HTH
                    Vince


                    Comment

                    • Vince Morgan

                      #11
                      Re: using echo but need something to execute php code

                      "Vince Morgan" <vinhar@REMOVEo ptusnet.com.auw rote in message
                      news:46348271$0 $22843$afc38c87 @news.optusnet. com.au...
                      Ooops, I always forget to refresh before I post.
                      I'm safe here, I'm never right;]


                      Comment

                      • Jon Slaughter

                        #12
                        Re: using echo but need something to execute php code


                        Well, I thought I was clear enough. I think what happens is because I use a
                        simple example that many here think there must be a simple solution. I give
                        a simple example only to express the problem as concisely as I can instead
                        of cluttering it up with things that are irrelevant and, I would assume,
                        only cause more confusion. Its like a math problem, If there is something
                        I'm trying to solve but the equation is very complicated and I can reduce
                        the issue down to a similar but much more simple problem then thats what I
                        would do. I believe my original explination was simple and explained the
                        problem but I could be wrong. I have gotten a solution from Janwillem which
                        probalby will work(haven't tried it yet due to other issues).

                        If it was simple as using include then I would have done it. Peopl elike
                        Toby and Geoff have a very simplistic view of things and there problem is
                        that their ego always ends up showing up.

                        In any case, again, I have a file that is essentially html code in it. It
                        has a php extension as all my files do. That is not the issue.

                        The issue is that I have this file, call it MyFile.php and I want to parse
                        it to modify some html code or generate some html code based on some
                        context. If I just use include then I have no way of looking at the file?

                        If I do

                        include 'MyFile.php'

                        Then how am I going to modify what I need to modify? Theres no way to get at
                        the contents of the file that is loaded do do anything.

                        For example, what if MyFile.php is

                        <body>
                        Hello
                        <div class="table"></div>
                        <?php
                        echo 'hello';
                        ?>
                        </body>

                        REMEMBER THIS IS JUST A SIMPLIFIED EXAMPLE. Obviously if this was what I was
                        really trying to do there would be no need to parse it as I could approach
                        it differently. (so don't tell me I need to approach it differently because
                        its actually more complicated and maybe I did things backwards but its too
                        late at this point)

                        In any case, now I have a file called AddTable.php that essentially takes
                        this file and parses it for <div class="table"an d inserts a table inside
                        that div.

                        if I simply do

                        include 'MyFile.php'

                        then how can I parse it?

                        if I do

                        $file = file_get_conten ts('MyFile.php' );

                        Then I can easily parse the string $file and modify it to add the table.

                        so after that I now have $file that looks something like
                        <body>
                        Hello
                        <table>
                        <tr>
                        <td>Blah
                        </td>
                        </tr>
                        </table>

                        <?php
                        echo 'hello';
                        ?>
                        </body>

                        and when I want to send it to the client,

                        echo $file;

                        it will not interpret the php code and just send it as text to them (so they
                        get exactly what is above in $file instead of

                        <body>
                        Hello
                        <table>
                        <tr>
                        <td>Blah
                        </td>
                        </tr>
                        </table>
                        hello
                        </body>


                        So I need a way to get $file to interpret the php code before I send it.
                        Janwillem has said eval will do it and from looking in the manual it seems
                        like it will work. There is also another solution I mentioned in the
                        original post that looks like it will work. I might have been able to design
                        the site differently and been able to avoid this issue but I didn't and I'm
                        not about to start over at this point just because some jack ass(not you)
                        gets to confused and doesn't understand the problem.

                        In any case I hope to try eval soon as I think it will be the easiest
                        solution and do exactly what I need.

                        Thanks,
                        Jon


                        Comment

                        • Jon Slaughter

                          #13
                          Re: using echo but need something to execute php code


                          "Geoff Berrow" <blthecat@ckdog .co.ukwrote in message
                          news:i8v8331561 747r64uuvvg6hdp lfoj71p2l@4ax.c om...
                          Message-ID: <5C_Yh.1820$tp5 .373@newssvr23. news.prodigy.ne tfrom Jon
                          Slaughter contained the following:
                          >
                          >>Your a dipshit...
                          >
                          No, that's /you're/ a dipshit.
                          >
                          >
                          >
                          Your a fucking genius... what about the rest of my post? You read that or
                          did you just scan it for spelling mistakes too?


                          Comment

                          • Vince Morgan

                            #14
                            Re: using echo but need something to execute php code

                            "Vince Morgan" <vinhar@REMOVEo ptusnet.com.auw rote in message
                            news:46348271$0 $22843$afc38c87 @news.optusnet. com.au...
                            parse($Page);
                            >[Or
                            >print parse($Page);
                            >Or
                            >echo parse($Page);
                            >]
                            There had to be an error somewhere ;]
                            "echo"ing, or "print"ing the output of "parse(&$Pa ge)" as defined won't work
                            as it doesn't actualy return anything.



                            Comment

                            • Vince Morgan

                              #15
                              Re: using echo but need something to execute php code

                              "Jon Slaughter" <Jon_Slaughter@ Hotmail.comwrot e in message
                              news:Be0Zh.6402 $2v1.3406@newss vr14.news.prodi gy.net...
                              The issue is that I have this file, call it MyFile.php and I want to parse
                              it to modify some html code or generate some html code based on some
                              context. If I just use include then I have no way of looking at the file?
                              >
                              If I do
                              >
                              include 'MyFile.php'
                              >
                              Then how am I going to modify what I need to modify? Theres no way to get
                              at
                              the contents of the file that is loaded do do anything.
                              >
                              For example, what if MyFile.php is
                              >
                              <body>
                              Hello
                              <div class="table"></div>
                              <?php
                              echo 'hello';
                              ?>
                              </body>
                              >
                              REMEMBER THIS IS JUST A SIMPLIFIED EXAMPLE. Obviously if this was what I
                              was
                              really trying to do there would be no need to parse it as I could approach
                              it differently. (so don't tell me I need to approach it differently
                              because
                              its actually more complicated and maybe I did things backwards but its too
                              late at this point)
                              >
                              In any case, now I have a file called AddTable.php that essentially takes
                              this file and parses it for <div class="table"an d inserts a table inside
                              that div.
                              >
                              if I simply do
                              >
                              include 'MyFile.php'
                              >
                              then how can I parse it?
                              >
                              if I do
                              >
                              $file = file_get_conten ts('MyFile.php' );
                              >
                              Then I can easily parse the string $file and modify it to add the table.
                              >
                              so after that I now have $file that looks something like
                              <body>
                              Hello
                              <table>
                              <tr>
                              <td>Blah
                              </td>
                              </tr>
                              </table>
                              >
                              <?php
                              echo 'hello';
                              ?>
                              </body>
                              >
                              and when I want to send it to the client,
                              >
                              echo $file;
                              >
                              it will not interpret the php code and just send it as text to them (so
                              they
                              get exactly what is above in $file instead of
                              >
                              <body>
                              Hello
                              <table>
                              <tr>
                              <td>Blah
                              </td>
                              </tr>
                              </table>
                              hello
                              </body>
                              >
                              >
                              I think eval() is going to be a little more complicated than you might
                              imagine, or I imagine perhaps.
                              However, may I suggest the following.
                              First, lets say your AddTable.php looks something like below;
                              also simplified;
                              --- begin AddTable.php
                              <?php
                              function makeTable(&$out put)
                              {
                              //simplified code to create the table output
                              $output='<table ><tr><td>tabl e stuff</td></tr></table>;
                              //$output now contains the table html .
                              }
                              //load MyFile into $output var below and call makeTable()
                              $output = file_get_conten ts('MyFile.php' );
                              makeTable($outp ut);
                              //$output now has the table html you want to display in the final
                              output.
                              echo $output; //echo'ing here will actualy happen at the place it is
                              included in the file below
                              ?>
                              -----.end AddTable.php

                              ----- begin Final HTML
                              <html>
                              <body>
                              <p>Hello<p>
                              <?php
                              include('AddTab le.php');
                              // AddTable.php is loaded here and the engine parses it immediately.
                              // After running the makeTable() function in AddTable.php it echo's
                              $output here
                              ?>
                              </body>
                              </html>
                              ----end final HTML

                              Final result;
                              <html>
                              <body>
                              Hello
                              <!-- output from include('AddTab le.php')
                              <table>
                              <tr>
                              <td>Blah
                              </td>
                              </tr>
                              </table>
                              --end output from the include
                              <?php
                              echo 'hello';
                              ?>
                              </body>
                              </html>

                              I think this is what you want and a lot less complicated than eval().
                              HTH
                              Vince


                              Comment

                              Working...