Link to home
Start Free TrialLog in
Avatar of interclubs
interclubs

asked on

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?
SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of interclubs
interclubs

ASKER

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.
For the values with the first example:

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

Open in new window

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

Open in new window

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...
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

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

Note that my solutions are based on preg_replace, not str_ireplace as in your previous examples.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
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)
interclubs, thanks for the question and the points.
hernst42, I see your reasoning for the \\ now, thanks for the info.
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!
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

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.
it's not $_GLOBALS it's $GLOBALS