You are here: Re: using echo but need something to execute php code « PHP Programming Language « IT news, forums, messages
Re: using echo but need something to execute php code

Posted by Vince Morgan on 04/29/07 13:20

"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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация