{foreach:newslist name="news"}
{/foreach}
如果上述标记有多个,那么我们该怎么区分?
这里要用到preg_replace
执行一个正则表达式搜索并且使用一个回调进行替换
捕获
(exp) 匹配exp,并捕获文本到自动命名的组里
(?exp) 匹配exp,并捕获文本到名称为name的组里,也可以写成(?’name’exp)
(?:exp) 匹配exp,不捕获匹配的文本,也不给此分组分配组号
零宽断言
(?=exp) 匹配exp前面的位置
(?<=exp) 匹配exp后面的位置
(?!exp) 匹配后面跟的不是exp的位置
(?
<?php require(LKPHP_PATH.'/MVC/Controller/cFunctions.inc'); //所有controller的父类 抽象类 abstract class _Master{ var $_view='index';//模板名称 var $_vars = array(); var $_cachetime=0;//缓存时间 function setView($viewName){ $this->_view = $viewName; } function getView(){ return LKPHP_PATH.'/MVC/View/'.LKPHP_VIEWPATH.'/'.$this->_view.'.'.LKPHP_EXTENSION; } function setVar($varName,$varValue){ //设置变量 $this->_vars[$varName] = $varValue; } function hasVarCache(){ if(the_cache($this->_view)){ return true; } return false; } function run(){ //解包变量 if($this->_cachetime > 0){ $getVars_cache = the_cache($this->_view); if($getVars_cache){ echo '<b>这是从memcache中获取的数据</b><br/>'; extract($getVars_cache); }else{ //同时要设置缓存 set_cache($this->_view,$this->_vars,0,$this->_cachetime); extract($this->_vars); } }else{ extract($this->_vars); } extract($this->_vars); if(LKPHP_IS_OPEN_FILE_CACHE){ //这里我们要讲写入缓冲区 $tpl=''; ob_start(); //加载头部模板 include(LKPHP_PATH.'/MVC/View/'.LKPHP_VIEWPATH.'/'.LKPHP_VIEWHEADER.'.'.LKPHP_EXTENSION); include($this->getView());//加载模板body include(LKPHP_PATH.'/MVC/View/'.LKPHP_VIEWPATH.'/'.LKPHP_VIEWFOOTER.'.'.LKPHP_EXTENSION);//尾部 $tpl = ob_get_contents(); ob_clean(); $file_name = md5($_SERVER['REQUEST_URI']); $cache_file='Cache/'.$file_name; if(file_exists($cache_file)){ echo file_get_contents($cache_file); }else{ file_put_contents($cache_file,$tpl); echo $tpl; } }else{ $tpl=''; ob_start(); //加载头部模板 include(LKPHP_PATH.'/MVC/View/'.LKPHP_VIEWPATH.'/'.LKPHP_VIEWHEADER.'.'.LKPHP_EXTENSION); include($this->getView());//加载模板body include(LKPHP_PATH.'/MVC/View/'.LKPHP_VIEWPATH.'/'.LKPHP_VIEWFOOTER.'.'.LKPHP_EXTENSION);//尾部 $tpl = ob_get_contents(); ob_clean(); //循环优先 $tpl = $this->genForeach($tpl); $tpl=$this->genSimpleVars($tpl); echo $tpl; } } function genForeach($tplCnt){ global $foreach_id; //对每一个foreach加上一个唯一标识符 $tplCnt = preg_replace_callback('/{(foreach):(\w{1,30})/is','foreach_callback',$tplCnt); $foreach_id = array_reverse($foreach_id);//对数组进行翻转操作 foreach ($foreach_id as $fid) { if(preg_match_all('/{foreach:(?<varObject>\w{1,30}?):'.$fid.'\s+name=\"(?<varName>\w{1,30}?)\"}/is',$tplCnt,$result)){ $finalResult = ""; var_dump($result); $varObject = $result['varObject'][0]; $varName = $result['varName'][0]; if(array_key_exists($varObject,$this->_vars)){ $pattern = '/{foreach:'.$varObject.':'.$fid.'\s+.*?}(?<replaceCnt>.*?){\/foreach}/is'; preg_match($pattern,$tplCnt,$cntResult); $cntResult = $cntResult['replaceCnt']; foreach($this->_vars[$varObject] as $row){ $tmp = $this->replaceForeachVar($cntResult,$varName,$row,$fid); $finalResult .= $tmp; } } //替换最终的某个foreach的值 $tplCnt = preg_replace('/{foreach:'.$varObject.':'.$fid.'\s+.*?}.*?{\/foreach}/is',$finalResult,$tplCnt); } } return $tplCnt; } function replaceForeachVar($cnt,$varName,$row,$fid){ //替换循环内部内容 if(preg_match_all('/{([^{]*?'.$varName.'\.(?<varValue>\w{1,30})[^}]*?)}/is',$cnt,$result)){ $varValue = $result['varValue'][0]; $result = $result[1]; foreach ($result as $r) { if($row[$varValue]){ if($varName.'.'.$varValue==trim($r)){//代表没有函数 $cnt = preg_replace('/{'.$varName.'\.'.$varValue.'}/is',$row[$varValue],$cnt); }else{ //代表有函数 $newr = preg_replace('/'.$varName.'\.'.$varValue.'/is', $row[$varValue], $r); eval('$last='.$newr.';'); if($last){ $cnt = str_replace('{'.$r.'}',$last,$cnt); } } } } } return $cnt; } //魔术方法 function __get($p){ $c=load_class($p); return $c; } //解析简单变量 function genSimpleVars($getTpl){ $pattern = "/\{\s*(\w{1,20})\s*\}/is"; if(preg_match_all($pattern,$getTpl,$result)){ $result = $result[1]; foreach ($result as $r) { if(array_key_exists($r, $this->_vars)){ $replace_pattern = "/\{\s*".$r."\s*\}/is"; $getTpl = preg_replace($replace_pattern,$this->_vars[$r],$getTpl); } } } return $getTpl; } } ?>