接触过最土的朋友应该很熟悉 最土模板解析功能主要由 文件完成 ,就是通过正则的替换,捕获来完成而已,与smarty的原理类似。下面分析下他的正则替换。
在template.php文件中有这段代码
<span 1</span> <span function</span> __parse(<span $tFile</span>,<span $cFile</span><span ) { </span><span 2</span> <span 3</span> <span $fileContent</span> = <span false</span><span ; </span><span 4</span> <span 5</span> <span if</span>(!(<span $fileContent</span> = <span file_get_contents</span>(<span $tFile</span><span ))) </span><span 6</span> <span return</span> <span false</span><span ; </span><span 7</span> <span 8</span> <span $fileContent</span> = <span preg_replace</span>( '/^(\xef\xbb\xbf)/', '', <span $fileContent</span> ); <span //</span><span EFBBBF </span> <span 9</span> <span $fileContent</span> = <span preg_replace</span>("/\<\!\-\-\s*\\\$\{(.+?)\}\s*\-\-\>/ies", "__replace('<?php \\1; ?>')", <span $fileContent</span><span ); </span><span 10</span> <span $fileContent</span> = <span preg_replace</span>("/\{(\\\$[a-zA-Z0-9_\[\]\\\ \-\'\,\%\*\/\.\(\)\>\'\"\$\x7f-\xff]+)\}/s", "<?php echo \\1; ?>", <span $fileContent</span><span ); </span><span 11</span> <span $fileContent</span> = <span preg_replace</span>("/\\\$\{(.+?)\}/ies", "__replace('<?php echo \\1; ?>')", <span $fileContent</span><span ); </span><span 12</span> <span $fileContent</span> = <span preg_replace</span>("/\<\!\-\-\s*\{else\s*if\s+(.+?)\}\s*\-\-\>/ies", "__replace('<?php } else if(\\1) { ?>')", <span $fileContent</span><span ); </span><span 13</span> <span $fileContent</span> = <span preg_replace</span>("/\<\!\-\-\s*\{elif\s+(.+?)\}\s*\-\-\>/ies", "__replace('<?php } else if(\\1) { ?>')", <span $fileContent</span><span ); </span><span 14</span> <span $fileContent</span> = <span preg_replace</span>("/\<\!\-\-\s*\{else\}\s*\-\-\>/is", "<?php } else { ?>", <span $fileContent</span><span ); </span><span 15</span> <span 16</span> <span for</span>(<span $i</span> = 0; <span $i</span> < 5; ++<span $i</span><span ) { </span><span 17</span> <span $fileContent</span> = <span preg_replace</span>("/\<\!\-\-\s*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\s*\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/loop\}\s*\-\-\>/ies", "__replace('<?php if(is_array(\\1)){foreach(\\1 AS \\2=>\\3) { ?>\\4<?php }}?>')", <span $fileContent</span><span ); </span><span 18</span> <span $fileContent</span> = <span preg_replace</span>("/\<\!\-\-\s*\{loop\s+(\S+)\s+(\S+)\s*\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/loop\}\s*\-\-\>/ies", "__replace('<?php if(is_array(\\1)){foreach(\\1 AS \\2) { ?>\\3<?php }}?>')", <span $fileContent</span><span ); </span><span 19</span> <span $fileContent</span> = <span preg_replace</span>("/\<\!\-\-\s*\{if\s+(.+?)\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/if\}\s*\-\-\>/ies", "__replace('<?php if(\\1){?>\\2<?php }?>')", <span $fileContent</span><span ); </span><span 20</span> <span 21</span> <span } </span><span 22</span> <span //</span><span Add for call <!--{include othertpl}--></span> <span 23</span> <span $fileContent</span> = <span preg_replace</span>("#<!--\s*{\s*include\s+([^\{\}]+)\s*\}\s*-->#i", '<?php include template("\\1");?>', <span $fileContent</span><span ); </span><span 24</span> <span 25</span> <span //</span><span Add value namespace</span> <span 26</span> <span if</span>(!<span file_put_contents</span>(<span $cFile</span>,<span $fileContent</span><span )) </span><span 27</span> <span return</span> <span false</span><span ; </span><span 28</span> <span 29</span> <span 30</span> <span return</span> <span true</span><span ; </span><span 31</span> <span } </span><span 32</span> <span 33</span> <span function</span> __replace(<span $string</span><span ) { </span><span 34</span> <span return</span> <span str_replace</span>('\"', '"', <span $string</span><span ); </span><span 35</span> }
现在对正则替换进行分析:
第8行
<span $fileContent</span> = <span preg_replace</span>( '/^(\xef\xbb\xbf)/', '', <span $fileContent</span> );
是过滤掉windows平台下utf8文件的特殊字符 ï » ¿
第九行
<span $fileContent</span> = <span preg_replace</span>("/\<\!\-\-\s*\\\$\{(.+?)\}\s*\-\-\>/ies", "__replace('<?php \\1; ?>')", <span $fileContent</span>);
规则浅析:
结合模式修正
i
e “替换后的内容”)
s
在{}里执行任何php代码
<span $fileContent</span> = <span preg_replace</span>("/\{(\\\$[a-zA-Z0-9_\[\]\\\ \-\'\,\%\*\/\.\(\)\>\'\"\$\x7f-\xff]+)\}/s", "<?php echo \\1; ?>", <span $fileContent</span>);
用于显示内容
第11行
<span $fileContent</span> = <span preg_replace</span>("/\\\$\{(.+?)\}/ies", "__replace('<?php echo \\1; ?>')", <span $fileContent</span>);
eg: ${ $a}对应
第12行
<span $fileContent</span> = <span preg_replace</span>("/\<\!\-\-\s*\{else\s*if\s+(.+?)\}\s*\-\-\>/ies", "__replace('<?php } else if(\\1) { ?>')", <span $fileContent</span>);
转为
<span $fileContent</span> = <span preg_replace</span>("/\<\!\-\-\s*\{elif\s+(.+?)\}\s*\-\-\>/ies", "__replace('<?php } else if(\\1) { ?>')", <span $fileContent</span>);
转为
<span $fileContent</span> = <span preg_replace</span>("/\<\!\-\-\s*\{else\}\s*\-\-\>/is", "<?php } else { ?>", <span $fileContent</span>);
模板里 转为
第17行
<span $fileContent</span> = <span preg_replace</span>("/\<\!\-\-\s*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\s*\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/loop\}\s*\-\-\>/ies", "__replace('<?php if(is_array(\\1)){foreach(\\1 AS \\2=>\\3) { ?>\\4<?php }}?>')", <span $fileContent</span>);
$v) { ?>
>
第18行与第17行差不多就是少了$k其他完全一样
第19行
<span $fileContent</span> = <span preg_replace</span>("/\<\!\-\-\s*\{if\s+(.+?)\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/if\}\s*\-\-\>/ies", "__replace('<?php if(\\1){?>\\2<?php }?>')", <span $fileContent</span>);