POST TIME:2017-11-13 00:35
正則匹配出所有的arclist標(biāo)簽并構(gòu)造SQL
取出數(shù)據(jù)并替換field為相應(yīng)字段的數(shù)據(jù)
為field標(biāo)簽添加一個function屬性
<?phprequire_once(dirname(__FILE__)."/../include/common.inc.php");//1.讀入模板文件$str=file_get_contents('test.html');//2.使用正則匹配出頁面中所有的arclist標(biāo)簽$re='/{dede:arclist(.*)}(.*){\/dede:arclist}/Us';//執(zhí)行正則匹配//第一個參數(shù):正則//第二個參數(shù):字符串//第三個參數(shù):這個函數(shù)會把匹配到的結(jié)果放第三個參數(shù)的數(shù)組中//返回值:匹配到的個數(shù)preg_match_all($re,$str,$a);//3.循環(huán)每一個匹配到的arclist標(biāo)簽進(jìn)行處理foreach($a[0]as$k=>$v){/****************處理標(biāo)簽1.根據(jù)標(biāo)簽上的屬性構(gòu)造一個SQL語句**************///取出標(biāo)簽相應(yīng)的屬性字符串并把屬性轉(zhuǎn)化成一個數(shù)組,如row="10"channelid="17"addfields="pffz,pfrs,yuyan"orderby="id"orderway="desc"$attrArr=strToArray($a[1][$k]);//根據(jù)構(gòu)造構(gòu)造SQL語句上的變量if(isset($attrArr['row']))$limit=$attrArr['row'];else$limit=20;if(isset($attrArr['orderby']))$orderby=$attrArr['orderby'];else$orderby='id';if(isset($attrArr['orderway']))$orderway=$attrArr['orderway'];else$orderway='desc';//連表的屬性if(isset($attrArr['channelid']))$leftJoin='LEFTJOINdede_addon17bONa.id=b.aid';else$leftJoin='';if(isset($attrArr['addfields']))$extraFields=','.$attrArr['addfields'];else$extraFields='';//解析屬性$sql="SELECTa.*$extraFieldsFROMdede_archivesa$leftJoinORDERBY$orderby$orderwayLIMIT$limit";$dsql->Execute('me',$sql);$html='';//每個arclist對應(yīng)的多個數(shù)據(jù)while($row=$dsql->GetArray('me')){//重置模板字符串,不要在原模板上面進(jìn)行替換$_tep=$a[2][$k];/**<li><imgsrc="[field:litpic/]"/><br/>標(biāo)題:[field:title/]<br/>評分分值:[field:pffzfunction="getSmallStar(@me)"/]<br/>評分人數(shù):[field:pfrs/]<br/>語言:[field:yuyan/]</li>**///把字符串中的[field:xxxx/]替換成$row['xxxx']變量。$_re='/\[field:(\w+)(\s+function=("|\&;)(\w+)\((.*)\)\3)?\/\]/U';preg_match_all($_re,$_tep,$_a);/**array0=>array0=>string'[field:litpic/]'(length=15)1=>string'[field:title/]'(length=14)2=>string'[field:pffz/]'(length=13)3=>string'[field:pfrs/]'(length=13)4=>string'[field:yuyan/]'(length=14)1=>array0=>string'litpic'(length=6)1=>string'title'(length=5)2=>string'pffz'(length=4)3=>string'pfrs'(length=4)4=>string'yuyan'(length=5)*///再循環(huán)每一條記錄中的每個字段的值foreach($_a[0]as$_k=>$_v){if($_a[4][$_k]=='')//把[field:xxx/]字符串替換成相應(yīng)的$row[xxx]變量$_tep=str_replace($_v,$row[$_a[1][$_k]],$_tep);else{//如果參數(shù)中有@me就把@me替換成當(dāng)前這個字段的數(shù)據(jù)庫中的值$_a[5][$_k]=str_replace('@me',$row[$_a[1][$_k]],$_a[5][$_k]);//如果有函數(shù)就調(diào)用函數(shù)$funStr='$_val='.$_a[4][$_k]."({$_a[5][$_k]});";eval($funStr);$_tep=str_replace($_v,$_val,$_tep);}}$html.=$_tep;}//到這arclist標(biāo)簽就已經(jīng)都解析成了相應(yīng)的數(shù)據(jù),把整個arclist標(biāo)簽替換成解析之后的數(shù)據(jù),$html就是
arclist最終解析完成之后的HTML的字符串$str=str_replace($v,$html,$str);//一個arclist就解析完成了}//把解析好的字符串生成前臺靜態(tài)頁file_put_contents('./index.html',$str);echo'解析成功!';functionstrToArray($str){$str=trim($str);//從屬性字符串中匹配出每個屬性,格式如:row="10"channelid="17"addfields="pffz,pfrs,yuyan"orderby="id"orderway="desc"$re='/[a-z]+\s*=\s*("|\&;).*\1/U';$data=array();preg_match_all($re,$str,$a);foreach($a[0]as$k=>$v){$_a=explode('=',$v);//去掉屬性值左右的引號,屬性左右的引號是單引號還是雙引號不能確定所以需要使用$a[1][$k]變量來代替,這個變量是正則中
的第一個括號的內(nèi)容,匹配的就是引號$_a[1]=ltrim($_a[1],$a[1][$k]);$_a[1]=rtrim($_a[1],$a[1][$k]);$data[$_a[0]]=$_a[1];}return$data;
}