how to make a placeholder

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
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

how to make a placeholder

Post by nite4000 »

got a fast issue

I need to take a variable like $var['sitename'];

but I want to be able to display that value by just entering [sitename] in the brackets.

Not sure anyone can help me with this or not
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: how to make a placeholder

Post by mecha_godzilla »

Hi,

Are you trying to populate a page template i.e.

1. Find every instance where a line contains [????]
2. Substitute it for the contents of $var[????]
3. Echo out the page template to the browser

HTH,

M_G
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: how to make a placeholder

Post by nite4000 »

actually that may be backwards. someone did it for me a long time ago where when I put [sitename] on the page it got the value of the $var['site_name'] from the database
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: how to make a placeholder

Post by mecha_godzilla »

I think the method will depend on how you access the information in your database, so I'm not really sure what to suggest - does something like the following code look useful:

Code: Select all

$placeholder_value = '1234';
$template_text = "For example, you could use a [placeholder_value]";
echo preg_replace('/\[(\w+)\]/e', "\$\\1", $template_text);
// output: For example, you could use a 1234
Where you have the replacement string, instead of substituting the placeholder for a variable of the same name, you could hard-code the name of the database object in there and match against a property of that object.

M_G
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: how to make a placeholder

Post by nite4000 »

that looks similar I am actually searching my 1TB drive trying to find where the code was if I still have it but that does look something like it

the variable is am using is $a_settings['sitename'];
in a page I have it where the text of the page is in a field in the database for easy editing for the user and in that text I want to just use [sitename] so they don't have to ask for the php code

I will try this and see how it works
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: how to make a placeholder

Post by mecha_godzilla »

If $a_settings is just an array of values then you could use the following code:

Code: Select all

// For testing purposes
$a_settings = array(); 
$a_settings['sitename'] = 'My Site';

$template_text = "Welcome to [sitename]";
echo preg_replace('/\[(\w+)\]/e', "\$a_settings['\\1']", $template_text);
// output: Welcome to My Site
M_G
Post Reply