PHP简单字符模板与参数匹配替换示例
//模板内容(含变量) $template = '尊敬的会员{user},请联系{server_qq}进行操作'; //模板参数值 $param = [ 'user' => '七歌', 'server_qq' => '10287093' ]; $content = preg_replace_callback('/\{[0-9a-z_]+\}/',function($match) use($param){ $t = substr($match[0],1,strlen($match[0])-2); if(isset($param[$t])) return $param[$t]; },$template); echo $content;
----------效果如下图------------
----------------
在做一些模板功能的时候可以使用,根据不同的匹配正则表达式会有不用结构的match,写好表达式后var_dump一下这个match 再考虑如何使用。