代码
1
<?
php
2
3 function parse_template( $tplfile , $objfile ) {
4 global $options ;
5
6 // 循环嵌套次数
7 $nest = 3 ;
8
9 // 打开模板文件
10 if ( ! $fp = fopen ( $tplfile , ‘rb’)) {
11 exit (’ Current template file not found or have no access ! ’);
12 }
13
14 $template = fread ( $fp , filesize ( $tplfile ));
15 fclose ( $fp );
16
17 // 匹配变量
18 //双引号(单引号)内的\具有转义所以,要得到一个\必须写为\\;要得到一个$必须写为\$;最后结果为\$,可在正则中使用的变量符号
19 $var_regexp = “((\\\$[a - zA - Z_\x7f - \xff][a - zA - Z0 - 9_\x7f - \xff] * )(\[[a - zA - Z0 - 9_\ - \ . \”\’\[\]\$\x7f - \xff] + \]) * )”;
20
21 // 匹配字符
22 $const_regexp = “([a - zA - Z_\x7f - \xff][a - zA - Z0 - 9_\x7f - \xff] * )”;
23
24 // 清除缩进(tab)
25 $template = preg_replace (” / ([\n\r] + )\t +/ s” , “\\ 1 “ , $template );
26
27 // 清除注释(<!– –>),方便后续操作,不需要匹配多余的<!–
28 $template = preg_replace (” / \ < \ ! \ - \ - \{( .+? )\}\ - \ - \ >/ s” , “{\\ 1 }” , $template );
29
30 // 将{LF}替换成一个硬回车(\n)
31 $template = str_replace (”{LF}” , “ <?= \”\\n\“ ?> ” , $template );
32
33 // 匹配多种变量形式,包括$xxx[”xxxx”]与$xxx[$xxx]、或$xxx
34 $template = preg_replace (” / \{(\\\$[a - zA - Z0 - 9_\[\]\’\”\$\ . \x7f - \xff] + )\} / s” , “ <?= \\ 1 ?> ” , $template );
35
36 // 使用/e修正符,可使替换元素以php代码执行后,再进行replace.
37 $template = preg_replace (” / $var_regexp / es” , “addquote(’ <?= \\ 1 ?> ’)” , $template );
38
39 // 再次替换叠加字符串变量
40 $template = preg_replace (” / \ < \ ? \ = \ < \ ? \ = $var_regexp \ ? \ > \ ? \ >/ es” , “addquote(’ <?= \\ 1 ?> ’)” , $template );
41
42 // 子模板嵌套解析
43 $template = preg_replace (” / [\n\r\t] * \{template\s + ([a - z0 - 9_] + )\}[\n\r\t] */ is” , “\n <? include template(’\\ 1 ′); ?> \n” , $template );
44 $template = preg_replace (” / [\n\r\t] * \{template\s + ( .+? )\}[\n\r\t] */ is” , “\n <? include template(\\ 1 ); ?> \n” , $template );
45
46 // eval语法解析
47 $template = preg_replace (” / [\n\r\t] * \{ eval \s + ( .+? )\}[\n\r\t] */ ies” , “stripvtags(’\n <? \\ 1 ; ?> \n’ , ”)” , $template );
48
49 // echo语法解析
50 $template = preg_replace (” / [\n\r\t] * \{ echo \s + ( .+? )\}[\n\r\t] */ ies” , “stripvtags(’\n <? echo \\ 1 ; ?> \n’ , ”)” , $template );
51
52 // elseif语法解析
53 $template = preg_replace (” / [\n\r\t] * \{ elseif \s + ( .+? )\}[\n\r\t] */ ies” , “stripvtags(’\n <? } elseif (\\ 1 ) { ?> \n’ , ”)” , $template );
54
55 // else语法解析
56 $template = preg_replace (” / [\n\r\t] * \{ else \}[\n\r\t] */ is” , “\n <? } else { ?> \n” , $template );
57
58 for ( $i = 0 ; $i < $nest ; $i ++ ) {
59 $template = preg_replace (” / [\n\r\t] * \{loop\s + (\S + )\s + (\S + )\}[\n\r] * ( .+? )[\n\r] * \{\ / loop\}[\n\r\t] */ ies” , “stripvtags(’\n <? if ( is_array (\\ 1 )) { foreach (\\ 1 as \\ 2 ) { ?> ’ , ' \n\\3\n<? } } ?>\n’)”, $template);
60 $template = preg_replace(”/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*(.+?)[\n\r\t]*\{\/loop\}[\n\r\t]*/ies”, “stripvtags(’\n<? if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>’, ' \n\\ 4 \n <? } } ?> \n’)” , $template );
61 $template = preg_replace (” / [\n\r\t] * \{ if \s + ( .+? )\}[\n\r] * ( .+? )[\n\r] * \{\ / if \}[\n\r\t] */ ies” , “stripvtags(’\n <? if (\\ 1 ) { ?> ’ , ' \n\\2\n<? } ?>\n’)”, $template);
62 }
63
64 //常量直接输出..
65 $template = preg_replace(”/\{$const_regexp\}/s”, “<?=\\1?>”, $template);
66
67 //相临定界符清除(使语法更加连贯)
68 $template = preg_replace(”/ \?\>[\n\r]*\<\? /s”, ” “, $template);
69
70 if(!@$fp = fopen($objfile, ‘wb’)) {
71 exit(’Directory \’./cache/template/\’ not found or have no access!’);
72 }
73
74 //转换链接中的&符号为&使编译模板读取时能够正常不会将其视为引用..
75 $template = preg_replace(”/\”(http)?[\w\.\/:]+\?[^\”]+?&[^\”]+?\”/e”, “transamp(’\\0′)”, $template);
76
77 flock($fp, 2);
78 fwrite($fp, $template);
79 fclose($fp);
80 }
81
82 //转换&避免&以引用方式执行..
83 function transamp($str) {
84 $str = str_replace(’&’, ‘&’, $str);
85 $str = str_replace(’&’, ‘&’, $str);
86 $str = str_replace(’\”‘, ‘”‘, $str);
87 return $str;
88 }
89
90 //将$var字符串,转换为可执行的php代码形式,并返回其结果..
91 //\”转换为”,将为[xxx]转换为[’xxx’]
92 function addquote($var) {
93 return str_replace(”\\\”", “\”", preg_replace(”/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s”, “[’\\1′]”, $var));
94 }
95
96 //设置语言变量
97 function languagevar($var) {
98 return $GLOBALS[’language’][$var] ? $GLOBALS[’language’][$var] : “!$var!”;
99 }
100 //清理或转换标签为php语法
101 function stripvtags($expr, $statement) {
102 $expr = str_replace(”\\\”", “\”", preg_replace(”/\<\?\=(\\\$.+?)\?\>/s”, “\\1“, $expr));
103 $statement = str_replace(”\\\”", “\”", $statement);
104 return $expr.$statement;
105 }
106
107 ?>
2
3 function parse_template( $tplfile , $objfile ) {
4 global $options ;
5
6 // 循环嵌套次数
7 $nest = 3 ;
8
9 // 打开模板文件
10 if ( ! $fp = fopen ( $tplfile , ‘rb’)) {
11 exit (’ Current template file not found or have no access ! ’);
12 }
13
14 $template = fread ( $fp , filesize ( $tplfile ));
15 fclose ( $fp );
16
17 // 匹配变量
18 //双引号(单引号)内的\具有转义所以,要得到一个\必须写为\\;要得到一个$必须写为\$;最后结果为\$,可在正则中使用的变量符号
19 $var_regexp = “((\\\$[a - zA - Z_\x7f - \xff][a - zA - Z0 - 9_\x7f - \xff] * )(\[[a - zA - Z0 - 9_\ - \ . \”\’\[\]\$\x7f - \xff] + \]) * )”;
20
21 // 匹配字符
22 $const_regexp = “([a - zA - Z_\x7f - \xff][a - zA - Z0 - 9_\x7f - \xff] * )”;
23
24 // 清除缩进(tab)
25 $template = preg_replace (” / ([\n\r] + )\t +/ s” , “\\ 1 “ , $template );
26
27 // 清除注释(<!– –>),方便后续操作,不需要匹配多余的<!–
28 $template = preg_replace (” / \ < \ ! \ - \ - \{( .+? )\}\ - \ - \ >/ s” , “{\\ 1 }” , $template );
29
30 // 将{LF}替换成一个硬回车(\n)
31 $template = str_replace (”{LF}” , “ <?= \”\\n\“ ?> ” , $template );
32
33 // 匹配多种变量形式,包括$xxx[”xxxx”]与$xxx[$xxx]、或$xxx
34 $template = preg_replace (” / \{(\\\$[a - zA - Z0 - 9_\[\]\’\”\$\ . \x7f - \xff] + )\} / s” , “ <?= \\ 1 ?> ” , $template );
35
36 // 使用/e修正符,可使替换元素以php代码执行后,再进行replace.
37 $template = preg_replace (” / $var_regexp / es” , “addquote(’ <?= \\ 1 ?> ’)” , $template );
38
39 // 再次替换叠加字符串变量
40 $template = preg_replace (” / \ < \ ? \ = \ < \ ? \ = $var_regexp \ ? \ > \ ? \ >/ es” , “addquote(’ <?= \\ 1 ?> ’)” , $template );
41
42 // 子模板嵌套解析
43 $template = preg_replace (” / [\n\r\t] * \{template\s + ([a - z0 - 9_] + )\}[\n\r\t] */ is” , “\n <? include template(’\\ 1 ′); ?> \n” , $template );
44 $template = preg_replace (” / [\n\r\t] * \{template\s + ( .+? )\}[\n\r\t] */ is” , “\n <? include template(\\ 1 ); ?> \n” , $template );
45
46 // eval语法解析
47 $template = preg_replace (” / [\n\r\t] * \{ eval \s + ( .+? )\}[\n\r\t] */ ies” , “stripvtags(’\n <? \\ 1 ; ?> \n’ , ”)” , $template );
48
49 // echo语法解析
50 $template = preg_replace (” / [\n\r\t] * \{ echo \s + ( .+? )\}[\n\r\t] */ ies” , “stripvtags(’\n <? echo \\ 1 ; ?> \n’ , ”)” , $template );
51
52 // elseif语法解析
53 $template = preg_replace (” / [\n\r\t] * \{ elseif \s + ( .+? )\}[\n\r\t] */ ies” , “stripvtags(’\n <? } elseif (\\ 1 ) { ?> \n’ , ”)” , $template );
54
55 // else语法解析
56 $template = preg_replace (” / [\n\r\t] * \{ else \}[\n\r\t] */ is” , “\n <? } else { ?> \n” , $template );
57
58 for ( $i = 0 ; $i < $nest ; $i ++ ) {
59 $template = preg_replace (” / [\n\r\t] * \{loop\s + (\S + )\s + (\S + )\}[\n\r] * ( .+? )[\n\r] * \{\ / loop\}[\n\r\t] */ ies” , “stripvtags(’\n <? if ( is_array (\\ 1 )) { foreach (\\ 1 as \\ 2 ) { ?> ’ , ' \n\\3\n<? } } ?>\n’)”, $template);
60 $template = preg_replace(”/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*(.+?)[\n\r\t]*\{\/loop\}[\n\r\t]*/ies”, “stripvtags(’\n<? if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>’, ' \n\\ 4 \n <? } } ?> \n’)” , $template );
61 $template = preg_replace (” / [\n\r\t] * \{ if \s + ( .+? )\}[\n\r] * ( .+? )[\n\r] * \{\ / if \}[\n\r\t] */ ies” , “stripvtags(’\n <? if (\\ 1 ) { ?> ’ , ' \n\\2\n<? } ?>\n’)”, $template);
62 }
63
64 //常量直接输出..
65 $template = preg_replace(”/\{$const_regexp\}/s”, “<?=\\1?>”, $template);
66
67 //相临定界符清除(使语法更加连贯)
68 $template = preg_replace(”/ \?\>[\n\r]*\<\? /s”, ” “, $template);
69
70 if(!@$fp = fopen($objfile, ‘wb’)) {
71 exit(’Directory \’./cache/template/\’ not found or have no access!’);
72 }
73
74 //转换链接中的&符号为&使编译模板读取时能够正常不会将其视为引用..
75 $template = preg_replace(”/\”(http)?[\w\.\/:]+\?[^\”]+?&[^\”]+?\”/e”, “transamp(’\\0′)”, $template);
76
77 flock($fp, 2);
78 fwrite($fp, $template);
79 fclose($fp);
80 }
81
82 //转换&避免&以引用方式执行..
83 function transamp($str) {
84 $str = str_replace(’&’, ‘&’, $str);
85 $str = str_replace(’&’, ‘&’, $str);
86 $str = str_replace(’\”‘, ‘”‘, $str);
87 return $str;
88 }
89
90 //将$var字符串,转换为可执行的php代码形式,并返回其结果..
91 //\”转换为”,将为[xxx]转换为[’xxx’]
92 function addquote($var) {
93 return str_replace(”\\\”", “\”", preg_replace(”/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s”, “[’\\1′]”, $var));
94 }
95
96 //设置语言变量
97 function languagevar($var) {
98 return $GLOBALS[’language’][$var] ? $GLOBALS[’language’][$var] : “!$var!”;
99 }
100 //清理或转换标签为php语法
101 function stripvtags($expr, $statement) {
102 $expr = str_replace(”\\\”", “\”", preg_replace(”/\<\?\=(\\\$.+?)\?\>/s”, “\\1“, $expr));
103 $statement = str_replace(”\\\”", “\”", $statement);
104 return $expr.$statement;
105 }
106
107 ?>