Alex Alex - 5 months ago 9
PHP Question

PHP - replace a string with a variable named like the string

so the string is like this:

"bla bla bla {VARIABLE} bla bla"

when I use this string somewhere in a function I want to replace {VARIABLE} with $variable (or any other uppercase strings with wrapped within {} charcters). $variable (and any other variables) will be defined inside that function

Can i do this?

$TEST = 'one';
$THING = 'two';
$str = "this is {TEST} a {THING} to test";

$result = preg_replace('/\{([A-Z]+)\}/e', "$$1", $str);