0

I've recently inherited a number of php files that served as email templates. These php files consist of strings concatenated with php variables. I would like to find all the variable names used in the template files so that I'm able to replace these names with the proper tags for Jinja2 templates I'm using now. Here's a snippet from one of the templates:

<?php if($lang == 'EN'){ 
################################# ENGLISH ####################################-->
$html1 = '

Dear '.$firstname.',
<br /><br />
We are contacting you in regards your recent order # '.$custmordernum.'. TO COMPLETE YOUR ORDER WE NEED 
ADDITIONAL INFORMATION FROM YOU.  Your order will be on hold until WE RECEIVE THE FOLLOWING 
}
else { 
################################# SPANISH ####################################-->

I've had limited success using the following regular expression:

(\'.*?\..*?\..*?\')

I'd like to match everything between the periods. Can anyone suggest a better regular expression?

2
  • Your asking for a world of hurt... for example . "' . ' " . would be legal but probally cause problems for most regex without some sort of back referencing...
    – Stephen
    Commented Jul 11, 2013 at 18:19
  • $a_str="apples:".cost_of_apples(1.23,5.67)." and extras" seems like it would be problematic ... what are you trying to do this for ... this approach may not be the best one Commented Jul 11, 2013 at 19:51

3 Answers 3

0

Something like this:

text = "$email = 'hello '.$user1_35.' we would like to annoy you '.$Tod-ay.' for 20 minutes.';"
res = re.findall("'\.(\$[\w-]*)\.'", text)
print res #['$user1_35', '$Tod-ay']

Honestly though, you're probably better off going through by hand. This only works for this specific format and doesn't handle variable concatenation at the beginning/end of lines, if there are spaces between periods/quotes/variables, and probably other weird cases.

1
  • This worked well, but you are right, a lot of the tags have formatting differences that make it difficult to match those tags. Some contain spaces before/after the periods and others are arrays and contain brackets, both of which break this regular expression. Thanks for your help, though. This allowed me to find a subset of the tags so now I have a smaller haystack.
    – Brett
    Commented Jul 11, 2013 at 18:52
0

Try /\.\$(.*)\./

I used http://www.solmetra.com/scripts/regex/index.php to try it and I was able to match firstname and custmordernum

0

If you use the following, you also wont have to deal with the periods

(?<=\.)\$\w+?(?=\.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.