Jump to content


Photo

Executing a string perhaps?


  • Please log in to reply
4 replies to this topic

#1 benmay.org

benmay.org
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 16 July 2006 - 10:03 AM

Hey,
Here's the situation, I have a website with a template in /templates/boringtemplate/template.php
the template file is html with <?php echo($title) ?> along the way, normally, to display it dynamically, I just run all the queries, and include("templates/$templatedir/template.php") and it works great,

But, I am getting to the stage where my little cms cant be used by some clients due to them not having mysql etc.. I'm trying to figure out how I can generate a static page..

Here is what I've started on..

$filename ="static_$current_page.html";

$myFile= fopen($filename,'a'); 

if(! $myFile){ print ("File could not be opened."); exit;}
fputs($myFile, $string);
fclose($myFile);

Thats fine, it will write $string to the desired html file. But.. How Can I get the string to include the answers the the php. In other words, I think I need to execute the php file, and save the result as a html file...

#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 16 July 2006 - 10:04 AM

I could be wrong, but i think you need to use the eval() function:
http://uk2.php.net/eval

#3 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 16 July 2006 - 10:12 AM

$template = $whatever_source;
$template = str_replace('\\', '\\\\', $template);
$template = str_replace('"', '\\"', $template);
eval('$template = "' . $template . '";');

This will replace all variables within $whatever_source with their corresponding value in the calling script.

I suggest you use this method even for a mysql version, as it abstracts code from templates. (IMHO, an important thing to do.)
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#4 benmay.org

benmay.org
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 16 July 2006 - 10:19 AM

That got it!! Thanks, and thanks for the promt (within 5 mins) Reply!

#5 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 16 July 2006 - 10:26 AM

Also, when using eval, it is SO important to validate user input! This cannot be stressed enogh.
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users