Solved

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

Posted on 2013-12-03
2
295 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
ID: 39693607
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
ID: 39693641
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

789 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