实现foreach循环嵌套
<?php
require(LKPHP_PATH.'/MVC/Controller/cFunctions.inc');
$foreach_id = array();
$plugif_id=array();
function foreach_callback($match)
{
$id=md5(uniqid()+mt_rand());
global $foreach_id;
$foreach_id[]=$id;
return "{".$match[1].":".$match[2].":".$id;
}
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());
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());
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;
$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 = "";
$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'];
$index=1;
foreach($this->_vars[$varObject] as $row){
$system=array('rowNumber'=>$index);
$tmp = $this->replaceForeachVar($cntResult,$varName,$row,$fid,$system);
$index++;
$finalResult .= $tmp;
}
}
$tplCnt = preg_replace('/{foreach:'.$varObject.':'.$fid.'\s+.*?}.*?{\/foreach}/is',$finalResult,$tplCnt);
}
}
return $tplCnt;
}
function replaceForeachVar($cnt,$varName,$row,$fid,$system){
$row = array_merge($row,$system);
if(preg_match_all('/{(.*?)}/is',$cnt,$result)){
$result = $result[1];
foreach ($result as $r) {
if(!preg_match_all('/'.$varName.'\.(?<varValue>\w{1,30})/is',$r,$varResult))continue;
$varList = $varResult['varValue'];
if(count($varList) ===1 && $varName.'.'.$varList[0] === trim($r)){
$varValue = $varList[0];
if($row[$varValue]){
$cnt = preg_replace('/{'.$varName.'\.'.$varValue.'}/is',$row[$varValue],$cnt);
}
}else{
$newr = $r;
foreach($varList as $varValue){
if(!$row[$varValue]){
continue;
}
$newr = preg_replace('/'.$varName.'\.'.$varValue.'/is',$row[$varValue],$newr);
}
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;
}
}
?>
{foreach:memberinfo name="members"}
{members.username}<br/>
{foreach:info name="news"}
{genericinput('news.remark')}<br/>
{/foreach}
{/foreach}