Solved

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

Posted on 2013-12-03
2
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.

730 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