You can use the preg_replace modifier with e as:
$string = preg_replace('/{([a-zA-Z\_\-]*?)}/ie','$arr["$1"]',$string);
Perfect link
Using the e modifier, you can have any PHP expression in the replacement part of preg_replace .
Now why is your regular expression /{([a-zA-Z\_\-])*?}/i not working?
Did you put *? outside the copied parenthesis ( ) , as a result, you only capture the first character of the word found in { } .
Also note that you did not escape { and } , which are regular expression metacharacters used to specify the range quantifier {num} , {min,max} . But in your case there is no need to avoid them, because the regular expression mechanism can deduce from the context that { and } cannot be used as a range operator, since they do not have numbers in the required format inside them and, therefore, process them literally.
source share