开发者

PHP - regex parse template variable

开发者 https://www.devze.com 2023-03-30 08:56 出处:网络
I have to parse this template string variable, for example: $str = \"<p>Don\'t want to receive email notifications? %%UNSUBSCRIBE[\'Adjust your message settings\']%% values in your privacy.<

I have to parse this template string variable, for example:

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

开发者_开发问答My result should be this:

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

How to do this in php/regex?


Well heres a regex that matches that placeholder & captures 2 possible variables inside it which you might need:

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

Something like preg_replace("/%%(UNSUBSCRIBE)\[\'(.*?)\'\]%%/", "$1$2", $string) should replace the whole placeholder with just the variables (the $1 and $2 represent the captured matches)...

Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消