先上代码
private function moldforExt(){//模板单层For循环处理 $SQL=$this->sql_obj; $URL=$this->url_obj; require($this->dataFile); $con=$this->moldFileContent; $con=str_replace("\n","<#tmoldbr#>",$con); $preg_for_str="/<!\-\-\s*for\[([a-zA-Z_\-0-9]+):([a-zA-Z_\-0-9]+)\]\s*\-\->([^!]*)<!\-\-\s*endfor\s*\-\->/"; preg_match_all($preg_for_str,$con,$preg_for_match); $preg_for_num=count($preg_for_match[0]); for($for_tem_i=0;$for_tem_i<$preg_for_num;$for_tem_i++){ $tem_for_st=$preg_for_match[1][$for_tem_i]; $tem_for_ed=$preg_for_match[2][$for_tem_i]; if(isset($$tem_for_st)&&isset($$tem_for_ed)){ $for_tem_start=$$tem_for_st; $for_tem_end=$$tem_for_ed; $for_tem_con=""; for($for_tem_j=$for_tem_start;$for_tem_j<$for_tem_end;$for_tem_j++){ $tttem_for_com=$preg_for_match[3][$for_tem_i]; $tttem_for_com=str_replace("({".$tem_for_st."})",$for_tem_j,$tttem_for_com); $for_tem_con.=$tttem_for_com; } $con=str_replace($preg_for_match[0][$for_tem_i],$for_tem_con,$con); } } $this->moldFileContent=str_replace("<#tmoldbr#>","\n",$con); }
其实里面用到的还是基于正则表达式
在模板中所写的for循环样式如下
<!-- for[strat:end] --> ... HTML代码 ... <!-- endfor -->
将它写成html注释的形式是为了防止在只浏览模板文件时造成排版错误。
for循环的开始标识start会在模板数据文件中对$start的存在进行检测,这里面用到了PHP内部的可变变量,讲一个变量的值作为一个变量名来使用。