请问正则怎么替换多个变量呢?

新手上路,请多包涵

比如

{{$a + $B}}

我想把变量 a 和 变量 b 的值都替换掉。

请问要怎么做呢?

阅读 1.7k
2 个回答
<?php
$str='{{ b.id + $c.id }} ';
$result = preg_replace_callback('/{{\s*\$?\w+\.\w+(?:\s*\+\s*\$?\w+\.\w+\s*)*}}/m', function ($match) {
    return preg_replace('/(\$?\w+)\.(\w+)/m','$1[\'$2\']',$match[0]);
}, $str);
printf($result);
?>

preg_replace_callback

$string = preg_replace_callback('/\b$([\w_+])\b/', function ($match, $key) {

}, $string);
推荐问题