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['passwo rd']
and then anything like this: <!--__NAME__-->
would get replaced with this:
name=str_ireplace("<!--nam e-->", $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?
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['passwo
and then anything like this: <!--__NAME__-->
would get replaced with this:
name=str_ireplace("<!--nam
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For the values with the first example:
foreach ($_REQUEST as $reqkey=>$reqval) {
$TemplateFile=preg_replace('/\(\[('.$reqkey.')\]\)/e','$reqval',$TemplateFile);
}
For the second example:
$TemplateFile=preg_replace('/<!--somename-->/e','$somename',$TemplateFile);
ASKER
that didn't do it. For example I had been doing this:
$TemplateFile = str_ireplace("<!--newslett er-->", $newsletter, $TemplateFile);
$TemplateFile = str_ireplace("<!--markerid -->", $markerid, $TemplateFile);
$TemplateFile = str_ireplace("<!--affiliat eid-->", $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("<!--permissi ons-->", $permissions, $TemplateFile);
$TemplateFile = str_ireplace("<!--dateofbi rth-->", $dateofbirth, $TemplateFile);
$TemplateFile = str_ireplace("<!--ipaddres s-->", $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...
$TemplateFile = str_ireplace("<!--newslett
$TemplateFile = str_ireplace("<!--markerid
$TemplateFile = str_ireplace("<!--affiliat
$TemplateFile = str_ireplace("<!--country-
$TemplateFile = str_ireplace("<!--terms-->
$TemplateFile = str_ireplace("<!--phone-->
$TemplateFile = str_ireplace("<!--username
$TemplateFile = str_ireplace("<!--permissi
$TemplateFile = str_ireplace("<!--dateofbi
$TemplateFile = str_ireplace("<!--ipaddres
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);
}
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
)
Note that my solutions are based on preg_replace, not str_ireplace as in your previous examples.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);
As a side note I was able to reduce the \'s without issue:
$TemplateFile = preg_replace("/\(\[(\w+)\]
$TemplateFile = preg_replace("/<!--(\w+)--
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)
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.
hernst42, I see your reasoning for the \\ now, thanks for the info.
ASKER
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!
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+)
I tried changing it to:
$TemplateFile = preg_replace("/\\(\\[(\w+)
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";
?>
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.
[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
ASKER
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-->"
but as the list grows i would rather have it be automatic and save myself the work.