Solved

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

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
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 …

729 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