I have several variables like these:
$foo = '123';
$bar = 'bqwe';
I need to replace {$foo} and {$bar} in a string with the variables.
preg_replace('~\{\$(.*)?\}~sU', ${'\\1'}, $string);
This doesn’t work.
PS: The regex might not be correct. I haven’t tested it with several variables like {$asd} {$bbb}. I am testing with one variable now.
You should make the regex more specific. Make it match for \w+ word characters.
And then you were on the right track, but need the
/eeval modifier to make the variable lookup in the local scope work:So when it matches
foothe relacement string becomes${"foo"}which then is used as expression. (Whereas in your original code it was incorrectly tried before the regex executed.)