I have an associative array, and a text with variables names inside, surrounded with brackets like this :
$message = "The value of var is: {var} ...-";
My array is like this:
$array=array('var'=>$var);
(Obviously this array can be bigger, and I don't know the name of the variables inside of it). Here is what I'm trying to do: I want to replace the {var} in my message by it's value in my array, like this:
$newMessage = preg_replace('#\{(.+)\}#',$array["$1"],$message);
Anyone knows how to do it?
Thank you very much!
@Rizier123's comment helped me, here's the code I used to solve my problem:
$newMessage=preg_replace_callback('#\{(.+)\}#',function($match)use($array){
return $array[$match[1]];
},$message);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.