For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > April 2007 > using echo but need something to execute php code









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author using echo but need something to execute php code
Jon Slaughter

2007-04-28, 6:58 pm

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<?php echo 'Hello'; ?></body>' and actually interpret the php
code?

e.g., doing

echo '<body>blah<?php 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_contents($Page)
parse($Page)
save($Page)
include "$Page"


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

At

http://us2.php.net/include/


"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_contents to get the php code to execute?



Thanks,
Jon



Janwillem Borleffs

2007-04-28, 6:58 pm

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


Jon Slaughter

2007-04-28, 6:58 pm


"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:4633bec1$0$31213$dbd43001@news.euronet.nl...
> Jon Slaughter wrote:
>
> 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


Jeff North

2007-04-28, 10:00 pm

On Sat, 28 Apr 2007 22:11:45 GMT, in comp.lang.php "Jon Slaughter"
<Jon_Slaughter@Hotmail.com>
<BEPYh.1450$RX.384@newssvr11.news.prodigy.net> wrote:

>|
>| "Janwillem Borleffs" <jw@jwscripts.com> wrote in message
>| news:4633bec1$0$31213$dbd43001@news.euronet.nl...
>| > 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@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Toby A Inkster

2007-04-29, 7:58 am

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
http://tobyinkster.co.uk/
G of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Jon Slaughter

2007-04-29, 7:58 am


"Toby A Inkster" <usenet200703@tobyinkster.co.uk> wrote in message
news:d50eg4-q7c.ln1@ophelia.g5n.co.uk...
> Jon Slaughter wrote:
>
>
> 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.


Geoff Berrow

2007-04-29, 7:58 am

Message-ID: <UqZYh.2848$HX7.607@newssvr19.news.prodigy.net> from Jon
Slaughter contained the following:

>
>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/
Jon Slaughter

2007-04-29, 7:58 am


"Geoff Berrow" <blthecat@ckdog.co.uk> wrote in message
news:05s83311jemllsbmo9ur6rkrml4laptdip@
4ax.com...
> Message-ID: <UqZYh.2848$HX7.607@newssvr19.news.prodigy.net> from Jon
> Slaughter contained the following:
>
>
>
> 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...



Geoff Berrow

2007-04-29, 7:58 am

Message-ID: <5C_Yh.1820$tp5.373@newssvr23.news.prodigy.net> from 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/
Vince Morgan

2007-04-29, 7:58 am

"Jon Slaughter" <Jon_Slaughter@Hotmail.com> wrote in message
news:tPOYh.1739$tp5.630@newssvr23.news.prodigy.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...ml/aid/1/fid/40

> something that will take a string like
>
> '<body>blah<?php 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(myFileIDidStuffTooAMomentAgo.inc);
?>
</body>
</html>
------------------------------------fileName.php
>
> e.g., doing
>
> echo '<body>blah<?php 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 "interpreted by the php engine" but you
seem to indicate something else by the context here.

>
>
> file_get_contents($Page)
> parse($Page)
> save($Page)
> include "$Page"
>

The above could be written into a file with a .php extension as;
---------------------
<?php
$Page = file_get_contents('somepath/Page.txt')
function parse(&$Page)
{
//Losts of parsing code here
}
parse($Page);
file_put_contents($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_contents('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


Vince Morgan

2007-04-29, 7:58 am

"Vince Morgan" <vinhar@REMOVEoptusnet.com.au> wrote 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;]


Jon Slaughter

2007-04-29, 7:58 am


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"> and 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_contents('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 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


Jon Slaughter

2007-04-29, 7:58 am


"Geoff Berrow" <blthecat@ckdog.co.uk> wrote in message
news:i8v8331561747r64uuvvg6hdplfoj71p2l@
4ax.com...
> Message-ID: <5C_Yh.1820$tp5.373@newssvr23.news.prodigy.net> from Jon
> Slaughter contained the following:
>
>
> No, that's /you're/ a dipshit.
>
>
>


Your a XXXXing genius... what about the rest of my post? You read that or
did you just scan it for spelling mistakes too?


Vince Morgan

2007-04-29, 7:58 am

"Vince Morgan" <vinhar@REMOVEoptusnet.com.au> wrote 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(&$Page)" as defined won't work
as it doesn't actualy return anything.



Vince Morgan

2007-04-29, 6:59 pm

"Jon Slaughter" <Jon_Slaughter@Hotmail.com> wrote in message
news:Be0Zh.6402$2v1.3406@newssvr14.news.prodigy.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"> and 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_contents('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(&$output)
{
//simplified code to create the table output
$output='<table><tr><td>table stuff</td></tr></table>;
//$output now contains the table html .
}
//load MyFile into $output var below and call makeTable()
$output = file_get_contents('MyFile.php');
makeTable($output);
//$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('AddTable.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('AddTable.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


Vince Morgan

2007-04-29, 6:59 pm


"Vince Morgan" <vinhar@REMOVEoptusnet.com.au> wrote in message
news:46349b51$0$16555$afc38c87@news.optusnet.com.au...
I always make an error :[
> --> end output from the include


this got included by accident by pasting from your post
------
> <?php
> echo 'hello';
> ?>

---------
sorry about that


Jon Slaughter

2007-04-29, 6:59 pm

<snip>

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


I can't do that because I have written a somewhat complicate scheme where
most of my files are automatically generated.

Basically I created a script that will take a file that gives the menu items
for a nav menu and it will generate all the html and files references for
those links along with adding some code to modify those links depending on
which page it is at the moment.

Its not complicated but I don't want to go back at this point in time and
rewrite it and the two methods are incompatible. (that is, I cannot use
your way because it would mean I would have to rewrite a lot of stuff).

Utimately I think it probably would have been easier and maybe its not to
difficult to actually modify but I don't feel like doing more work than I
have to at this point. When I rewrite my site I'll try to do a better job
so I'll wait until I get to that point. But if I can't find a method that
works then I might have too... luckily that doesn't seem to be the case.

Thanks,
Jon


Jerry Stuckle

2007-04-29, 6:59 pm

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


Geoff,

You shouldn't call him a dipshit. That's an insult to dipshits everywhere.

He's just a typical XXXXXXX who doesn't know how to ask a question,
gives people just the information HE thinks we need to figure out the
answer HE wants. Then he blames everyone else for his inadequacies.

It's similar to the philosophy professor who, for the final exam, asks:

"What is the meaning of",

then flunks anyone who doesn't know he means:

"the graffiti scrawled on the wall on the northeast corner of 4th and Main".

If he were to learn how to ask question properly he would get good answers.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Jerry Stuckle

2007-04-29, 6:59 pm

Jon Slaughter wrote:
> 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"> and 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_contents('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 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
>
>


Jon,

Maybe if you would have explained it this way in the start you would
have gotten better answers. This is NOT the problem you started with!

As it is, I can think of a couple of ways to do it. But after seeing
the way you treat people trying to help you, I'm not inclined to post them.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Toby A Inkster

2007-04-29, 6:59 pm

Jon Slaughter wrote:

> file_get_contents($Page)
> parse($Page)
> save($Page)
> include "$Page"
>
> as it seems uncessary to have to write the page to disk just to include it.


Mount an area of RAM as a virtual disk.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
G of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Jon Slaughter

2007-04-29, 6:59 pm


"Toby A Inkster" <usenet200703@tobyinkster.co.uk> wrote in message
news:1sseg4-q7c.ln1@ophelia.g5n.co.uk...
> Jon Slaughter wrote:
>
>
> Mount an area of RAM as a virtual disk.
>


Ok, thats something new, I haven't done anything like that. I'll have to
look into it.

Thanks,
Jon


Jerry Stuckle

2007-04-29, 9:58 pm

Jon Slaughter wrote:
>
> No, thats the point. he didn't answer it. He answered the question he
> thought I asked and then when I said it wasn't Geoff has to jump in and give
> me some BS.
>


If that wasn't the question you asked, then you are not clear with your
question. IME Geoff is pretty knowledgeable and accurate in his response.

>
> yeah, and if thats all you read of my post then obviously your going to post
> the wrong thing. Maybe its not clear because you fail to read the full post?
>


Or maybe it's not clear because you don't explain thoroughly?

> How bout reading the full post? Is that to much to ask?
>


I did. And I came to the same conclusion Geoff did.

> "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"
>
> If that isn't clear then I don't know what is.
>


That's perfectly clear. As clear as mud.

>
> I said that. I'm not going to bother wasting energy quoting my original post
> but if you bother to actually look at it then you'll see thats exactly what
> I said. It might not have been clear but it was obviously clearn enough for
> Janwillem.
>
>


And I'm not going to bother wasting my energy trying to answer someone
who 1) doesn't make his question clear, and 2) attacks people trying to
help him.

Even if I got paid for this (I don't), I've put in too many years to put
up with your BS.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com