[thelist] PHP: How to include a variable in DB text?

Tim Fountain tim at incutio.com
Sat Jul 5 08:00:48 CDT 2003

On Friday, July 4, 2003 at 13:15, you wrote:

> What I want to accomplish is that I can include a variable in the
> text, i. e. type it into the DB form, say

> Lorem ipsum dolor sit $var   --->  submit  --->
> $text = "Lorem ipsum dolor sit $foo";

> ... and retrieve the text from the DB so that $foo is being
> filled in with its value:

> $foo = "amet";
> print( "$text" );   --->  Lorem ipsum dolor sit amet

The other reply to your message contains the best solution - use
str_replace, providing you have a limited range of variables that you
want replacing.  Alternatively, take a look at the eval() function in
the manual.  That will allow you to evaluate your string as PHP code,
at which point the variables will be replaced by their current values.

It looks like you're trying to write a basic templating system. There
are quite a few of these around already (e.g. http://smarty.php.net),
and a lot of articles on them (some questioning the need for them at

Tim Fountain | Web developer | Incutio Limited | www.incutio.com
email: tim at incutio.com | Tel: +44 8708 700 333 | Fax: +44 7092 181 581

More information about the thelist mailing list