正则表达式匹配了{$name} {age}...
$this->_vars[]我用这个数组吧匹配的东西保存
private function parVar(){
$_patten='/\{\$([\w]+)\}/';//匹配模板中的{变量}
if(preg_match($_patten,$this->_tpl,$mat)){
//模板内容替换后付给$this->_tpl
$this->_tpl=preg_replace($_patten,"<?php echo \$this->_vars['$1'];?>",$this->_tpl);
}
}
为什么这里面"<?php echo \$this->_vars['$1'];?>"能用 $1来表示?
这里的
$1
不是PHP
的变量, 而是preg_replace
的占位符,表示正则表达式匹配出来的结果(1表示第一个匹配,也就是'/\{\$([\w]+)\}/'
中()
取出的那些字符)