Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 868
Alex Avatar asked Sep 21 '10 19:09

Alex


1 Answers

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

$result = preg_replace('/\{([A-Z]+)\}/e', "$$1", $str);
like image 86
Alex Howansky Avatar answered Oct 21 '22 04:10

Alex Howansky