yep, I had that problem at one stage. I was storing full SQL queries in the database, but with $variables in my WHERE clauses etc...... very simple way to do it. Basically you have to tell PHP to run over the code twice.... once to re-populate your variables with their values within that scope, and then you can do as you please with it.
so say you have a variable called $stuff and it contains the following string:
Hello, my name is $name
...and in the current scope, $name is equal to rich81.
now, if you just print($stuff);, the output will be:
Hello, my name is $name
but if you do the following:
eval("\$stuff=\"$stuff\";");
print($stuff);
... php will run over the code once BEFORE printing it. the slashes in the eval() function look messy but it's necessary.
so the sequence of events:
Hello, my name is $name
<eval the string>
Hello, my name is rich81
<print the string>