PHP - 正则表达式解析模板变量

时间:2011-08-23 18:48:52

标签: php regex

我必须解析这个模板字符串变量,例如:

$str = "<p>Don't want to receive email notifications? %%UNSUBSCRIBE['Adjust your message settings']%% values in your privacy.</p>";

我的结果应该是这样的:

"<p>Don't want to receive email notifications? <a href='http://www.example.com/'>Adjust your message settings</a> values in your privacy.</p>"

如何在php / regex中执行此操作?

1 个答案:

答案 0 :(得分:3)

嗯,这是一个匹配该占位符的正则表达式&amp;在其中捕获您可能需要的2个可能变量:

%%(UNSUBSCRIBE)\[\'(.*?)\'\]%%

preg_replace("/%%(UNSUBSCRIBE)\[\'(.*?)\'\]%%/", "$1$2", $string)这样的东西应该只用变量替换整个占位符($ 1和$ 2代表捕获的匹配)...

祝你好运!