Forum Moderators: coopster

Message Too Old, No Replies

Execute PHP Code Stored in Database

         

Anyango

5:23 pm on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Techs

a simple problem

i have HTML code stored in my database fields , i just want to have a script so that whenever that script is called it executes that code as PHP Code.. i am sure your first answer would be eval() but this is a bitt different problem.

leme explain. Lets say we have this code

<html><head><title><? ShowBodyOfThePage();?>
</title></head>
<body>
<? ShowBodyOfThePage();?>
</body>
</html>

Can any one share his expetise of how we can write a script to which if i send this string

"<html><head><title><? ShowTitleOfThePage();?>
</title></head>
<body>
<? ShowBodyOfThePage();?>
</body>

</html>"

it returns to me the same html but with that php function getting executed and replaced with whatever the function returns.

Please feel free to advice if the same thing can be done in some other way i mean if for example your method needs to remove "<?>" tags or anything or put this string in any echo.

Assistance is Much needed and will be Highly Appreciated.

Anyango

6:36 pm on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Kinda Funny but i found the solution myself

Posting it here, in case anyone else needs it:

<?
$content="<?echo 'PHP:yes this works! <br>'?>HTML:Yes this Works too!";

$tmpfname = tempnam ("/tmp", "FOO");
$fp = fopen($tmpfname, "w");
fwrite($fp, $content);
fclose($fp);
include($tmpfname);
unlink($tmpfname);
?>

BarryStCyr

8:23 pm on Jul 14, 2005 (gmt 0)

10+ Year Member



You could use the eval function ala

<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
?>