Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_替换为数组索引_Php_Regex_Preg Replace - Fatal编程技术网

Php preg_替换为数组索引

Php preg_替换为数组索引,php,regex,preg-replace,Php,Regex,Preg Replace,我有一个关联数组和一个包含变量名的文本,用括号括起来,如下图所示: $message = "The value of var is: {var} ...-"; 我的数组是这样的: $array=array('var'=>$var); $newMessage = preg_replace('#\{(.+)\}#',$array["$1"],$message); (显然,这个数组可以更大,我不知道其中变量的名称)。下面是我要做的:我想用数组中的值替换消息中的{var},如下所示: $ar

我有一个关联数组和一个包含变量名的文本,用括号括起来,如下图所示:

$message = "The value of var is: {var} ...-";
我的数组是这样的:

$array=array('var'=>$var);
$newMessage = preg_replace('#\{(.+)\}#',$array["$1"],$message);
(显然,这个数组可以更大,我不知道其中变量的名称)。下面是我要做的:我想用数组中的值替换消息中的{var},如下所示:

$array=array('var'=>$var);
$newMessage = preg_replace('#\{(.+)\}#',$array["$1"],$message);
有人知道怎么做吗

多谢各位

帮助了我,以下是我用来解决问题的代码:

$newMessage=preg_replace_callback('#\{(.+)\}#',function($match)use($array){
    return $array[$match[1]];
},$message);

看看:
preg\u replace\u callback()