Page 1 of 1

Saving a Variable in DB

Posted: Thu Apr 30, 2009 1:33 pm
by watson516
I am working on a content editor for a website and I would like to allow the admin to put in variables(the currently logged in user's username) into the page content. How would I go about doing this so that it echos the username instead of $username?

I have attempted to use eval but it doesn't seem to work with html tags. I have also tryed to serialize the variable but when I go to echo the unserialized version, it doesn't echo anything. Any ideas?

Re: Saving a Variable in DB

Posted: Thu Apr 30, 2009 1:50 pm
by watson516
I have found a workable solution. I use a place holder and then change it just before I display the content using str_replace

Re: Saving a Variable in DB

Posted: Thu Apr 30, 2009 1:51 pm
by Christopher
Typically you would use embedded tags, much like mail-merge in word processors.

Code: Select all

$str = 'Hello {username}, welcome.';
$output = str_replace('{username}', $username, $str);