I have string which contains a variable in curly braces and I want to replace them with a value.
$text = 'Hi My Name is ##{Name}## and I am ##{Adjective}##';
preg_match_all('/{([^#]+)}/i', $text, $matches);
foreach ($matches[1] as $key => $value) {
$text = str_replace('{' . $value . '}', 'SomeValue', $text);
}
print_r($matches[1]);
print_r(str_replace('##', '', $text));
OUTPUT
Array ( [0] => Name [1] => Adjective )
Hi My Name is SomeValue and I am SomeValue
But I am not able to handle deifferent variations of the string for example.
1. $text = 'Hi My Name is ##{Name}{Adjective}##'
2. $text = 'Hi My Name is ##{Name}and I am{Adjective}##'
3. $text = 'Hi My Name is ##{Name}, {Adjective}##'
4. $text = 'Hi My Name is ##{Name} {Adjective}##'
I would want the similar result in array output so that values can be replaced
Array ( [0] => Name [1] => Adjective )
NOTE: I am able to ensure that '##' will always be present at the start and end of the curly braces, but not necessarily in between the braces e.g. point 1,2,3,4 above in example string.