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?
interclubsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.