Solved

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

Posted on 2013-12-03
2
288 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

810 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