phpcms有自己的一套模板機(jī)制,代碼如下:
- function template_parse($str, $istag = 0)
- {
- $str = preg_replace("/([/n/r]+)/t+/s","//1",$str);
- $str = preg_replace("//</!/-/-/{(.+?)/}/-/-/>/s", "{//1}",$str);
- $str = preg_replace("//{template/s+(.+)/}/","<?php include template(//1); ?>",$str); 相當(dāng)于
- $str = preg_replace("//{include/s+(.+)/}/","<?php include //1; ?>",$str);
- $str = preg_replace("//{php/s+(.+)/}/","<?php //1?>",$str);
- $str = preg_replace("//{if/s+(.+?)/}/","<?php if(//1) { ?>",$str);
- $str = preg_replace("//{else/}/","<?php } else { ?>",$str);
- $str = preg_replace("//{elseif/s+(.+?)/}/","<?php } elseif (//1) { ?>",$str);
- $str = preg_replace("//{//if/}/","<?php } ?>",$str);
- $str = preg_replace("//{loop/s+(/S+)/s+(/S+)/}/","<?php if(is_array(//1)) foreach(//1 AS //2) { ?>",$str);
- $str = preg_replace("//{loop/s+(/S+)/s+(/S+)/s+(/S+)/}/", "<?php if(is_array(//1)) foreach(//1 AS //2 => //3) { ?>",$str);
- $str = preg_replace("//{//loop/}/","<?php } ?>",$str);
- $str = preg_replace("//{//get/}/","<?php } unset(/$DATA); ?>",$str);
- $str = preg_replace("//{tag_([^}]+)/}/e", "get_tag('//1')", $str);
- $str = preg_replace("//{get/s+([^}]+)/}/e", "get_parse('//1')", $str);
- $str = preg_replace("//{([a-zA-Z_/x7f-/xff][a-zA-Z0-9_/x7 f-/xff:]*/(([^{}]*)/))/}/","<?php echo //1;?>",$str);
- $str = preg_replace("//{//$([a-zA-Z_/x7f-/xff][a-zA-Z0-9_ /x7f-/xff:]*/(([^{}]*)/))/}/","<?php echo //1;?>",$str);
- $str = preg_replace("//{(//$[a-zA-Z_/x7f-/xff][a-zA-Z0-9_/x7f-/xff]*)/}/","< ?php echo //1;?>",$str);
- $str = preg_replace("//{(//$[a-zA-Z0-9_/[/]/'/"/$/x7f-/xff]+)/}/es", "addquote('<?php echo //1;?>')",$str);
- $str = preg_replace("//{([A-Z_/x7f-/xff][A-Z0-9_/x7f-/xff ]*)/}/s", "<?php echo //1;?>",$str);
- if(!$istag) $str = "<?php defined('IN_PHPCMS') or exit('Access Denied'); ?>".$str; //Vevb.com
- return $str;
- }
其中先容幾個(gè)關(guān)鍵點(diǎn):
/s 是將 代表任意符號(hào)的"."變成任意的符號(hào)。dedecms /s 是空格的意思.
$str = preg_replace("/([/n/r]+)/t+/s","//1",$str); 取括號(hào)中一個(gè)或者多個(gè)回車換行的字符并且外面有至少一個(gè)制表符的字符.
$str = preg_replace("//</!/-/-/{(.+?)/}/-/-/>/s", "{//1}",$str); //這是將注釋父里面變成大括號(hào).
$str = preg_replace("//{include/s+(.+)/}/","<?php include //1; ?>",$str); //輸進(jìn)的 include 將被替換成 <?php include ?>
$str = preg_replace("//{php/s+(.+)/}/","<?php //1?>",$str); //例如 {php echo "a"} 將被替換成 <?php echo "a";?>
$str = preg_replace("//{if/s+(.+?)/}/","<?php if(//1) { ?>",$str);
$str = preg_replace("//{else/}/","<?php } else { ?>",$str); 這個(gè)事講{if } {else} 替換<?php if ($a='1') echo a; else {a='b'}?> 替換的是 if +空格 +一個(gè)或者多個(gè)字符 if (//1)是取括號(hào)里的值也就是內(nèi)存地址的值
$str = preg_replace("//{loop/s+(/S+)/s+(/S+)/}/","<?php if(is_array(//1)) foreach(//1 AS //2) { ?>",$str);
$str = preg_replace("//{loop/s+(/S+)/s+(/S+)/s+(/S+)/}/", "<?php if(is_array(//1)) foreach(//1 AS //2 => //3) { ?>",$str); // 這連個(gè)似乎拿出來有點(diǎn)疏漏http://www.pcbaike.net/,大家可以研究研究,測試時(shí)沒有合適的主要是取第一個(gè)括號(hào)中或者第二個(gè)括號(hào)中的數(shù)據(jù)假如是數(shù)組替換成foreach循環(huán) $str = preg_replace("//{get/s+([^}]+)/}/e", "get_parse('//1')", $str);
這個(gè)事替換get標(biāo)簽實(shí)質(zhì)上是調(diào)用一個(gè) get_parse 函數(shù),有參函數(shù).
|
新聞熱點(diǎn)
疑難解答
圖片精選