简体   繁体   English

使用python正则表达式查找php字符串连接

[英]Find php string concatenation using python regex

I've recently inherited a number of php files that served as email templates. 我最近继承了许多用作电子邮件模板的php文件。 These php files consist of strings concatenated with php variables. 这些php文件由与php变量串联的字符串组成。 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. 我想找到模板文件中使用的所有变量名,以便能够用我现在使用的Jinja2模板的适当标签替换这些名称。 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? 谁能建议一个更好的正则表达式?

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

I used http://www.solmetra.com/scripts/regex/index.php to try it and I was able to match firstname and custmordernum 我使用http://www.solmetra.com/scripts/regex/index.php进行了尝试,并且能够匹配firstnamecustmordernum

如果使用以下内容,则也不必处理句点

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

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. 如果句点/引号/变量之间存在空格,并且可能还有其他奇怪的情况,那么这仅适用于这种特定格式,并且不会在行的开头/结尾处处理变量串联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM