Saving a Variable in DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Saving a Variable in DB

Post 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?
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Saving a Variable in DB

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Saving a Variable in DB

Post 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);
(#10850)
Post Reply