Author Topic: Executing a string perhaps?  (Read 602 times)

0 Members and 1 Guest are viewing this topic.

Offline benmay.orgTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Executing a string perhaps?
« on: July 16, 2006, 06:03:38 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..

Code: [Select]

$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...

Offline GingerRobot

  • Guru
  • Fanatic
  • *
  • Posts: 4,133
  • Gender: Male
  • Call me Ben
    • View Profile
Re: Executing a string perhaps?
« Reply #1 on: July 16, 2006, 06:04:44 AM »
I could be wrong, but i think you need to use the eval() function:
http://uk2.php.net/eval

Offline Joe Haley

  • Enthusiast
  • Posts: 103
  • Gender: Male
    • View Profile
Re: Executing a string perhaps?
« Reply #2 on: July 16, 2006, 06:12:37 AM »
Code: [Select]
$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

Offline benmay.orgTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Re: Executing a string perhaps?
« Reply #3 on: July 16, 2006, 06:19:57 AM »
That got it!! Thanks, and thanks for the promt (within 5 mins) Reply!

Offline Joe Haley

  • Enthusiast
  • Posts: 103
  • Gender: Male
    • View Profile
Re: Executing a string perhaps?
« Reply #4 on: July 16, 2006, 06:26:08 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