Solved

Using preg_replace to insert variables into a loaded template file... best way?

Posted on 2013-12-03
2
281 Views
Last Modified: 2013-12-03
Hi,
I'm loading a template file into a div using jquery and populating in the example below "%FIRST_NAME%" with the first name from my $user_data object. Works fine.

Problem is, I have about 10 or so different variables to insert into the template (vehicle, address etc. etc.) What Im wondering is what is the 'right' or condensed way of doing it? Only way I can think of is repeating a few of the lines over and over somehow. Is there a smart way of doing this?

	$template = file_get_contents($template_url);
	$pattern = '%FIRST_NAME%';
	$replacement = $user_data->first_name;
	$rendered_template = preg_replace($pattern, $replacement, $template);
	
	echo $rendered_template;

Open in new window

0
Comment
Question by:tjyoung
2 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
Comment Utility
I would do something like:

$mapping = array(
 "FIRST_NAME" => $user_data->first_name,
 "LAST_NAME" => $user_data->last_name
);

$rendered_template = $template;
foreach($mapping as $key => $replacement)
{
  $rendered_template = preg_replace("/%{$key}%/", $replacement, $rendered_template);
}

Open in new window


You -could- make it more dynamic and try to automatically search user_data for any matching variables between %% signs so %FOO% would search for $user_data->foo, but that can be a security risk. Unless you are going to constantly be adding new variables to the template, it's better to predefine the list.

Also, I'd actually use str_replace instead of preg_replace if you're not going to use actual patterns:

foreach($mapping as $key => $replacement)
{
  $rendered_template = str_replace("%{$key}%", $replacement, $rendered_template);
}

It'll be much faster.
0
 
LVL 1

Author Closing Comment

by:tjyoung
Comment Utility
Thats awesome, worked perfect and str_replace is the way to go. Tried it once but kept getting an error so switched. Tried yours and works perfect. Thanks very much.
ps. love the username by the way
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now