Preg_replace ('/{get_ (\w+)}/', $arr [$], $content);
For a back reference, I need to pass the matching \w+ as a parameter, to the custom function, or as an index to the array.
Reply to discussion (solution)
Preg_replace_callback can callback the function, but how to match to, as the index of the array, this I can't find the method
Preg_replace ('/{get_ (\w+)}/e ', ' $arr [' $ '] ', $content);
$content = ' {get_abc} '; $arr [' abc '] = 123;echo preg_replace ('/{get_ (\w+)}/e ', ' $arr ["$"] ', $content);
The extended mode E represents Eval, which means that the second argument is executed as a PHP statement
However, this approach was abolished by the php5.5, because the dynamic interpretation of PHP statements There is a problem of efficiency and security
A reasonable approach is to use Preg_replace_callback
The following are 3 of the wording
$content = ' {get_abc} '; $arr [' abc '] = 123;echo preg_replace_callback ('/{get_ (\w+)}/', ' Back ', $content); function back ($ m) { global $arr; return $arr [$m [1];}
$content = ' {get_abc} '; $arr [' abc '] = 123;echo preg_replace_callback ('/{get_ (\w+)}/', create_function (' $m ', ' global $ Arr return $arr ["$m [1]"]; '), $content);
$content = ' {get_abc} '; $arr [' abc '] = 123;echo preg_replace_callback ('/{get_ (\w+)}/', function ($m) use ($arr) {return $ arr["$m [1]"];}, $content);
Clearly the 3rd is the clearest, but only in the php5.3 began to support