我有一组示例用户字符串输入,其中可能有也可能没有智能变量,这些变量就像使用{literal} {/ literal}标记一样转义。
Array
(
[0] => {$PLEASE}
[1] => {PLEASE}
[2] => {{PLEASE}}
[3] => {{{PLEASE}}}
[4] => {a{PLEASE}}
[5] => {a{$PLEASE}}
[6] => {{$PLEASE}a}
[7] => {{PLEASE}a}
[8] => {{{$PLEASE}}}
[9] => {{{{PLEASE}}}}
)
这是我希望实现的目标。
Array
(
[0] => {$PLEASE}
[1] => {literal}{PLEASE}{/literal}
[2] => {literal}{{PLEASE}}{/literal}
[3] => {literal}{{{PLEASE}}}{/literal}
[4] => {literal}{a{PLEASE}{/literal}
[5] => {literal}{a{/literal}{$PLEASE}{literal}}{/literal}
[6] => {literal}{{/literal}{$PLEASE}{literal}a}{/literal}
[7] => {literal}{PLEASE}a}{/literal}
[8] => {literal}{{{/literal}{$PLEASE}{literal}}}{/literal}
[9] => {literal}{{{{PLEASE}}}}{/literal}
)
现在我有了这个
$data = preg_replace('/{+([^\$])([a-z0-9]*)}+/si', '{literal}{\1\2}{/literal}', $data);
哪个给了我
Array
(
[0] => {$PLEASE}
[1] => {literal}{PLEASE}{/literal}
[2] => {literal}{PLEASE}{/literal}
[3] => {literal}{PLEASE}{/literal}
[4] => {a{literal}{PLEASE}{/literal}
[5] => {a{$PLEASE}}
[6] => {{$PLEASE}a}
[7] => {literal}{PLEASE}{/literal}a}
[8] => {{{$PLEASE}}}
[9] => {literal}{PLEASE}{/literal}
)
现在已经停留了很长一段时间,想知道是否有人可以帮我搞清楚,或者是否有可能这样做。
答案 0 :(得分:0)
//Step 1: Replace 'real' smarty variables with an intermediate string
$data1 = preg_replace('/{(\$\w+)}/', "!!!$1!!!", $arr);
//replace start and end curly braces with {literal}:
$data2 = preg_replace('/{(.*)}/', '{literal}{$1}{/literal}', $data);
//Replace all inner smarty variables with their original string:
$data3 = preg_replace('/.!!!(.*)!!!/', '{/literal}$1{literal}', $data2);
//Replace standalone variables with their original string:
$data4 = preg_replace('/^!!!(.*)!!!$/', '{$1}', $data3);
您可以在一个命令中合并步骤3和4