• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

Parsing a template and replacing variables

I have a template file contained in a variable called $TemplateFile
It looks like this:

name=<!--name-->
password=([password])
etc...

I am looking for an elegant way to avoid hard coding the entire thing, but what needs to happen is this:

anything in ([__NAME__]) need to replaced with the $_REQUEST('__NAME__').
So this:
password=([password])
would be this:
password=$_REQUEST['password']

and then anything like this: <!--__NAME__-->
would get replaced with this:
name=str_ireplace("<!--name-->", $name, $TemplateFile);

So my hope is that I can just pass $TemplateFile off, and have the whole thing automatically parsed and returned to me with all the variable filled in, as opposed to having to write the whole thing out by hand and trying to account for each possible variable.

Does that make sense?
0
interclubs
Asked:
interclubs
  • 10
  • 3
  • 3
2 Solutions
 
ddrudikCommented:
For your first requirement:
<?php
echo preg_replace("/\(\[(.*?)\]\)/","$_REQUEST('$1')",$sourcestring);
?>

Please explain your second requirement further.
0
 
interclubsAuthor Commented:
essentially the same as the first, only instead of being a request object, it is an already declared variable.

So for example, <!--somename--> would be replaced with the value of a variable called $somename.

I had been hard coding it like this:
str_ireplace("<!--name-->", $name, $TemplateFile);

but as the list grows i would rather have it be automatic and save myself the work.
0
 
ddrudikCommented:
For the values with the first example:

foreach ($_REQUEST as $reqkey=>$reqval) {
  $TemplateFile=preg_replace('/\(\[('.$reqkey.')\]\)/e','$reqval',$TemplateFile);
}

Open in new window

0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
ddrudikCommented:
For the second example:
$TemplateFile=preg_replace('/<!--somename-->/e','$somename',$TemplateFile);

Open in new window

0
 
interclubsAuthor Commented:
that didn't do it. For example I had been doing this:
$TemplateFile = str_ireplace("<!--newsletter-->", $newsletter, $TemplateFile);
$TemplateFile = str_ireplace("<!--markerid-->", $markerid, $TemplateFile);
$TemplateFile = str_ireplace("<!--affiliateid-->", $affiliateid, $TemplateFile);
$TemplateFile = str_ireplace("<!--country-->", $country, $TemplateFile);
$TemplateFile = str_ireplace("<!--terms-->", $terms, $TemplateFile);
$TemplateFile = str_ireplace("<!--phone-->", $phone, $TemplateFile);
$TemplateFile = str_ireplace("<!--username-->", $username, $TemplateFile);
$TemplateFile = str_ireplace("<!--permissions-->", $permissions, $TemplateFile);
$TemplateFile = str_ireplace("<!--dateofbirth-->", $dateofbirth, $TemplateFile);
$TemplateFile = str_ireplace("<!--ipaddress-->", $ipaddress, $TemplateFile);

and I would like to somehow do it with just one expression as opposed to having to hardcode all this. Maybe it isn't possible...
0
 
ddrudikCommented:
If you had the variables/values for the second example in an array called $vararray you could use:
foreach ($vararray as $reqkey=>$reqval) {
  $TemplateFile=preg_replace('/<!--'.$reqkey.'-->/e','$reqval',$TemplateFile);
}

Open in new window

0
 
ddrudikCommented:
Note, for the second example to work you need a $vararray array with the variable names and values:
Array
(
    [newsletter] => newletter val
    [markerid] => markerid val
)

Open in new window

0
 
ddrudikCommented:
Note that my solutions are based on preg_replace, not str_ireplace as in your previous examples.
0
 
hernst42Commented:
Following a solution without iterating over an array

$TemplateFile = '<!--foo-->([__NAME__])';
$TemplateFile = preg_replace("/\\(\\[(\w+)\\]\\)/e", '$_REQUEST["\1"]', $TemplateFile);
$TemplateFile = preg_replace("/\\<\\!--(\w+)--\\>/e", '$\1', $TemplateFile);

Open in new window

0
 
ddrudikCommented:
hernst42, nice extension of the concept, sure is cleaner code.
As a side note I was able to reduce the \'s without issue:
$TemplateFile = preg_replace("/\(\[(\w+)\]\)/e", '$_REQUEST["\1"]', $TemplateFile);
$TemplateFile = preg_replace("/<!--(\w+)-->/e", '$\1', $TemplateFile);
0
 
hernst42Commented:
this might work, but might also fail. special regex-char like < !( must be prefix with a \ and when using double quote the correct way to create a \ in the string is to use \\. In the Zend Code Analyzer your code gives me:

Bad escape sequence: \( (line 3)
Bad escape sequence: \[ (line 3)
Bad escape sequence: \w (line 3)
Bad escape sequence: \] (line 3)
Bad escape sequence: \) (line 3)
Bad escape sequence: \w (line 4)
0
 
ddrudikCommented:
interclubs, thanks for the question and the points.
hernst42, I see your reasoning for the \\ now, thanks for the info.
0
 
interclubsAuthor Commented:
One quick follow up:

if I wanted to replace this one to get it to work with global variables, as opposed to request, how would i do it:
$TemplateFile = preg_replace("/\\(\\[(\w+)\\]\\)/e", '$_REQUEST["\1"]', $TemplateFile);
I tried changing it to:
$TemplateFile = preg_replace("/\\(\\[(\w+)\\]\\)/e", '$_GLOBALS["\1"]', $TemplateFile);

but it didn't work.

Thanks!
0
 
ddrudikCommented:
interclubs, you may want to check your $_GLOBALS array, it worked fine for me:
<?php
$_GLOBALS['__NAME__']='name data';
$foo='foo data';
$TemplateFile = '<!--foo-->
([__NAME__])';
$TemplateFile = preg_replace("/\\(\\[(\w+)\\]\\)/e", '$_GLOBALS["\1"]', $TemplateFile);
$TemplateFile = preg_replace("/\\<\\!--(\w+)--\\>/e", '$\1', $TemplateFile);
echo "<pre>$TemplateFile";
?>

Open in new window

0
 
ddrudikCommented:
As a side note, the given pattern uses \w which translates into:
[a-zA-Z_0-9]

This assumes all of your variable names do not include characters outside of that set, such as "-" etc.
0
 
hernst42Commented:
it's not $_GLOBALS it's $GLOBALS
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 10
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now