设为首页 加入收藏

TOP

UCenterHome1.5的模板语法解析(二)
2014-11-23 22:19:27 来源: 作者: 【 】 浏览:10
Tags:UCenterHome1.5 模板 语法 解析
s选项确定的

如space_doing.htm里有一个被替换成了

*/

//开始处理

//变量

$var_regexp = "((\$[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)([[a-zA-Z0-9_-."\[]$x7f-xff]+])*)";

/*

上面用红色显示的是转义符,没用红色显示的\就还是反斜杠

但是一开始为什么要用三个,我搞不明白。试验了一下,\$匹配的就是$,而改成$却没能匹配模板文件里的变量名。这个我还没搞明白。

$var_regexp匹配变量名

[a-zA-Z_x7f-xff] 变量名以字母或下划线或汉字开头

x7f-xff这个让人非常困惑,到底是想匹配什么呢?难道是想匹配扩展ASCII码吗?0x7f-0xff确实是扩展ASCII码表的范围,但显然没人会用这些控制字符去当变量名。

我这个UCH是UTF-8版本的,从UCS-2(就是现在通用的Unicode)到UTF-8的编码方式如下:

UCS-2编码(16进制)
UTF-8 字节流(二进制)

0000 - 007F
0xxxxxxx

0080 - 07FF
110xxxxx 10xxxxxx

0800 - FFFF
1110xxxx 10xxxxxx 10xxxxxx

前面的0000-007F是兼容ASCII码的

汉字的unicode编码在0080-FFFF里,转换成UTF-8后的字节是

110xxxxx或10xxxxxx或1110xxxx,在0x01111111-0x11111111范围内,也即0x7f-0xff。

更精确地,这个匹配变量名开头的部分可以写成[a-zA-Z_xc0-xef],因为汉字的开始字节只可能是110xxxxx或1110xxxx,范围就是0xc0-0xdf和0xe0-0xef

[a-zA-Z0-9_x7f-xff]* 比变量名的开头字节的允许取值范围多了数字,和C语言是一样的,变量名不能以数字开头

更精确地,这里可以写成[a-zA-Z0-9_x80-xef]* 因为这里汉字的字节可能是110xxxxx或1110xxxx或10xxxxxx,比0xc0-0xef多了10xxxxxx,即0x80-0xbf,合起来就是0x80-0xef

([[a-zA-Z0-9_-."\[]$x7f-xff]+])*

变量可能是数组形式的,如$name1[$name2],$[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*匹配了$name1,后面的[$name2]怎么匹配呢?我一开始以为是[$[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]],因为[]里面也应该是一个变量名。

但是变量可能是嵌套数组形式的,如$name1[$name2[$name3[$name4]]],此时用$[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]来一个个匹配变量名明显不可行。所以只是用[]把可能的字符都框起来,希望程序员不要写太扯淡的变量名了。

*/

$template = preg_replace("//s", "{\1}", $template);

/*

形如的字符串被替换成{name}

前面 解析广告,时间处理等产生的等都不匹配,仍然保留

name就是(.+ ) 这里的.匹配包括换行符在内的所有字符(有/s选项) 表示懒惰匹配

*/

$template = preg_replace("/([ ]+) +/s", "\1", $template);

/*

去掉换行回车后的制表符

beyondcompare中的对比效果如下


*/

$template = preg_replace("/(\$[a-zA-Z0-9_[]\"$x7f-xff]+).([a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)/s", "\1[\2]", $template);

/*

形如$name1.name2替换成$name1[name2],不要搞成了$name1[name2],\2和"\1"都是正则表达式的后向引用。可是我在模板文件中还没找到例子

*/

$template = preg_replace("/{(\$[a-zA-Z0-9_[]\"$.x7f-xff]+)}/s", "< =\1 >", $template);

/*

形如{$name1}替换成< =$name1 >

如space_doing.htm里有{$_SN[$space[uid]]}被换成了

< =$_SN[$space[uid]] >

*/

$template = preg_replace("/$var_regexp/es", "addquote(< =\1 >)", $template);

/*

将没有被{}包裹的变量名替换成addquote(< =\1 >)

addquote的作用是将类似[name]转换成[‘name’]

如space_doing.htm里有{if $space}被替换成了{if < =$space >}

但是这样也有副作用,比如space_doing.htm里有{$_SN[$space[uid]]},在上一条语句中被替换成了< =$_SN[$space[uid]] >,在这又被替换成了< =< =$_SN[$space[uid]] > >

*/

$template = preg_replace("/< =< =$var_regexp > >/es", "addquote(< =\1 >)", $template);

/*

消除上一条语句的副作用,将< =< =$_SN[$space[uid]] > >换成< =$_SN[$space[uid]] >

*/

//逻辑

$template = preg_replace("/{elseifs+(.+ )}/ies", "stripvtags(< php } elseif(\1) { >,)", $template);

/*

{elseif expression1} 替换成stripvtags(< php } elseif(expression1) { >,)

stripvtags的作用是将类似< =$name >替换成$name

*/

$template = preg_replace("/{else}/is", "< php } else { >", $template);

/*

{else} 替换成< php } else { >

*/

//循环

for($i = 0; $i < 5; $i++) {

$template = preg_replace("/{loops+(S+)s+(S+)}(.+ ){/loop}/ies", "stripvtags(< php if(is_array(\1)) { foreach(\1 as \2) { >,\3< php } } >)", $template);

/*

解析loop

将类似{loop array1 key1} expression1 {/loop}替换成

stripvtags(

<’ php if(is_array(array1)) {foreach (array1 as key1){ >’,

‘expression1< php } } >’

如space_doing.htm里将

{if < =$_TPL[titles] >}{loop < =$_TPL[titles] > < =$value >}{if < =$value >}< =$value > - {/if}{/loop}{/if}{if < =$space >}< =$_SN[$space[uid]] > - {/if}< =$_SCONFIG[sitename] ></p> <p>替换成了</p> <p><title>{if < =$_TPL[titles] >}< php if(is_array($_TPL[titles])) { foreach($_TPL[titles] as $value) { >{if < =$value >}< =$value > - {/if}< php } } >{/if}{if < =$space >}< =$_SN[$space[uid]] > - {/if}< =$_SCONFIG[sitename] ></p> <p>*/</p> <p>$template = preg_replace("/{loops+(S+)s+(S+)s+(S+)}(.+ ){/loop}/ies", "stripvtags(< php if(is_array(<a href="file:/1">\1</a>)) { foreach(<a href="file:/1">\1</a> as <a href="file:/2">\2</a> => <a href="file:/3">\3</a>) { >,\4< php } } >)", $template);</p> <p>/*</p> <p>解析loop</p> <p>将类似{loop array1 key1 value1} expression1 {/loop}替换成</p> <p>stripvtags(</p> <p><’ php if(is_array(array1)) {foreach (array1 as key1=>value1){ >’,</p> <p>‘expression1< php } } >’</p> <p>)</p> <p>如space_doing.htm里将</p> <p>{loop < =$moodlist > < =$key > < =$value >}</p> <p><li></p> <p><div class="avatar48"><a href="space.php uid=< =$value[uid] >&do=doing"><img src="<!--AVATAR_TAG_14-->" alt="< =$_SN[$value[uid]] >" /></a></div></p> <p><p><a href="space.php uid=< =$value[uid] >" title="< =$_SN[$value[uid]] >">< =$_SN[$value[uid]] ></a></p></p> <p><p class="time"><!--DATE_TAG_9--></p></p> <p></li></p> <p>{/loop}</p> <p>替换成了</p> <p>< php if(is_array($moodlist)) { foreach($moodlist as $key => $value) { ></p> <p><li></p> <p><div class="avatar48"><a href="space.php uid=< =$value[uid] >&do=doing"><img src="<!--AVATAR_TAG_14-->" alt="< =$_SN[$value[uid]] >" /></a></div></p> <p><p><a href="space.php uid=< =$value[uid] >" title="< =$_SN[$value[uid]] >">< =$_SN[$value[uid]] ></a></p></p> <p><p class="time"><!--DATE_TAG_9--></p></p> <p></li></p> <p>< php } } ></p> <p>*/</p> <p>$template = pr</span></font></td> </tr> </table> <!--//投票--> <table width="98%" border="0" cellspacing="0" cellpadding="0" style='TABLE-LAYOUT: fixed;WORD-WRAP: break-word;' align="center"> <tr> </tr> <tr> <td colspan="2" align="center" class="page" height="25"><a href="bencandy.php?fid=45&aid=23039&page=1" title="首页">首页</A> <a href="bencandy.php?fid=45&aid=23039&page=1" title="上一页">上一页</A> <a href="bencandy.php?fid=45&aid=23039&page=1" title="第1页">1</a> <a href='#'><font color=red>2</font></a> <a href="bencandy.php?fid=45&aid=23039&page=3" title="第3页">3</a> <a href="bencandy.php?fid=45&aid=23039&page=4" title="第4页">4</a> <a href="bencandy.php?fid=45&aid=23039&page=3" title="下一页">下一页</A> <a href="bencandy.php?fid=45&aid=23039&page=4" title="尾页">尾页</A> <a href='#'><font color=red>2</font>/4/4</a></td> </tr> <tr align="right"> <td colspan="2" height="25" > <SCRIPT LANGUAGE="JavaScript" src="https://www.cppentry.com/images/default/bencandy.js"></SCRIPT> 【<a href="javascript:doZoom(18)">大</a> <a href="javascript:doZoom(14)">中</a> <a href="javascript:doZoom(12)">小</a>】【<a href="javascript:doPrint()">打印</a>】 <input type=hidden value=1 name="h1" id="h1"> 【<a href="javascript:ft(1)" id="Maiweb1">繁体</a>】【<a href="https://www.cppentry.com/member/post.php?job=postnew&fid=45" target=_blank>投稿</a>】【<a href="https://www.cppentry.com/do/job.php?job=collect&fid=45&id=23039">收藏</a>】 【<a href="https://www.cppentry.com/do/job.php?job=recommend&fid=45&id=23039" target=_blank>推荐</a>】【<a href="https://www.cppentry.com/do/job.php?job=report&fid=45&id=23039" target=_blank>举报</a>】【<a href="https://www.cppentry.com/do/comment.php?fid=45&id=23039" target=_blank>评论</a>】 【<a href="javascript:window.close()">关闭</a>】 【<a href="javascript:window.close()"></a><a href="#">返回顶部</a>】</td> </tr> <tr> <td colspan="2" style="text-align:right"> <div> <div style="float:right"> <div class="bdsharebuttonbox"><a href="#" class="bds_more" data-cmd="more"></a><a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a><a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a><a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博"></a><a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网"></a><a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a></div> <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"32"},"share":{},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到:","viewSize":"16"},"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["qzone","tsina","tqq","renren","weixin"]}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script> </div> <div style="float:right; font-size:14px">分享到: </div> </div> </td> </tr> <tr class="nextpage"> <td width="50%" align="left"><a href="bencandy.php?fid=45&id=23040" onclick="">上一篇</a>:<a href="bencandy.php?fid=45&id=23040" onclick="" title="#define的一些用法">#define的一些用法</a></td> <td width="50%" align="right" height="25"><a href="bencandy.php?fid=45&id=23038" onclick="">下一篇</a>:<a href="bencandy.php?fid=45&id=23038" onclick="" title="开始聊聊C#泛型和委托(一)">开始聊聊C#泛型和委托(一)</a></td> </tr> </table> </td> </tr> <tr> <td class="foot"> <h3 class="L"></h3> <h3 class="R"></h3> </td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" class="dragTable" id="view_article_bbs"> <tr> <td class="head"> <h3 class="L"></h3> <span class="TAG"> </span> <h3 class="R"></h3> </td> </tr> <tr> <td class="middle"> <script type="text/javascript" id="wumiiRelatedItems"></script><div style="display:none"><!--访问统计,这段代码,会加快内容的打开速度,但是会影响右边列表的打开速度,<script src='http://pw.cnzz.com/c.php?id=80674837' language='JavaScript' charset='gb2312'></script>--></div></td> </tr> <tr> <td class="foot"> <h3 class="L"></h3> <h3 class="R"></h3> </td> </tr> </table> <!-- --> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="dragTable" id="commentTable"> <tr> <td class="head"> <h3 class="L"></h3> <span class="TAG">评论</span> <h3 class="R"></h3> </td> </tr> <tr> <td class="middle"> <SCRIPT LANGUAGE="JavaScript"> <!-- document.write('<span id="comment_show"><img alt="内容加载中,请稍候..." src="https://www.cppentry.com/images/default/ico_loading3.gif"></span>'); document.write('<div style="display:none;"><iframe src="https://www.cppentry.com/do/comment_ajax.php?fid=45&aid=23039&iframeID=comment_show" width=0 height=0 name="comment_show"></iframe></div>'); //--> </SCRIPT> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <form name="form_comment" id="form_comment" method="post" target="comment_show" action="https://www.cppentry.com/do/comment_ajax.php?fid=45&aid=23039&iframeID=comment_show"> <tr style="display:" id="comment_username_tr"> <td width="16%"><span class="L">帐  号:</span></td> <td width="84%"><span class="R"> <input type="text" name="username" id="comment_username" size="12"> 密码: <input type="password" name="password" id="comment_password" size="12"> (<a href="https://www.cppentry.com/do/reg.php" target="_blank"><u>新用户注册</u></a>)</span></td> </tr> <tr style="display:" id="comment_yzimg_tr"> <td width="16%"><span class="L">验 证 码:</span></td> <td width="84%"> <input id="yzImgNum" type="text" name="yzimg" size="8"> <SCRIPT LANGUAGE="JavaScript"> <!-- document.write('<img border="0" id="yz_Img" name="imageField" onclick="this.src=this.src+Math.random();" src="https://www.cppentry.com/do/yzimg.php?'+Math.random()+'">'); //--> </SCRIPT> </td> </tr> <tr> <td width="16%"><span class="L">表  情:</span></td> <td width="84%"> <style type="text/css"> <!-- .selected {filter:Alpha(opacity=100);border:1px solid #FF9900} .unselected {filter:Alpha(opacity=50);border:1px solid #EDF8DD} --> </style> <SCRIPT LANGUAGE="JavaScript"> <!-- var prevIcon; function icon(num){ num.className="selected"; if(typeof(prevIcon)!="undefined"){ prevIcon.className="unselected"; }else{ document.all.firstface.className="unselected"; } if(num.className=="unselected"){ num.className="selected"; } prevIcon=num; document.getElementById("commentface").value=num.childNodes(0).id ; } //--> </SCRIPT> <table border=0 cellspacing=0 cellpadding=0> <tr> <td class="selected" onClick="icon(this)" id="firstface" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/1.gif" width="20" height="20" id="1"></td> <td class="unselected" onClick="icon(this)" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/2.gif" width="20" height="20" id="2"></td> <td class="unselected" onClick="icon(this)" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/3.gif" width="20" height="20" id="3"></td> <td class="unselected" onClick="icon(this)" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/4.gif" width="20" height="20" id="4"></td> <td class="unselected" onClick="icon(this)" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/5.gif" width="20" height="20" id="5"></td> <td class="unselected" onClick="icon(this)" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/6.gif" width="20" height="20" id="6"></td> <td class="unselected" onClick="icon(this)" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/7.gif" width="20" height="20" id="7"></td> <td class="unselected" onClick="icon(this)" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/8.gif" width="20" height="20" id="8"></td> <td class="unselected" onClick="icon(this)" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/9.gif" width="20" height="20" id="9"></td> <td class="unselected" onClick="icon(this)" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/10.gif" width="20" height="20" id="10"></td> <td class="unselected" onClick="icon(this)" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/11.gif" width="20" height="20" id="11"></td> <td class="unselected" onClick="icon(this)" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/12.gif" width="20" height="20" id="12"></td> <td class="unselected" onClick="icon(this)" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/13.gif" width="20" height="20" id="13"></td> <td class="unselected" onClick="icon(this)" style="cursor:hand"><img src="https://www.cppentry.com/images/default/faceicon/14.gif" width="20" height="20" id="14"></td> <td align="center" valign="top"> <input name="commentface" type="hidden" value="1"> </td> </tr> </table> </td> </tr> <tr> <td width="16%"><span class="L">内  容:</span></td> <td width="84%"><span class="R"> <textarea name="content" cols="50" rows="5" id="comment_content" onKeyDown="quickpost(event)"></textarea> </span></td> </tr> <tr> <td width="16%"> <script language="JavaScript"> <!-- cnt = 0; function quickpost(event){ if((event.ctrlKey && event.keyCode == 13)||(event.altKey && event.keyCode == 83)){ cnt++; if (cnt==1){ post_comment(); }else{ alert('内容正在提交...'); } } } function post_comment(){ if(document.getElementById("comment_yzimg_tr").style.display==''){ if(document.getElementById("yzImgNum").value==''){ alert('验证码不能为空!'); return false; } } if(document.getElementById("comment_content").value==''){ alert('内容不能为空!'); return false; } document.getElementById("form_comment").submit(); document.getElementById("comment_content").value=''; if(document.getElementById("yzImgNum")!=null){ document.getElementById("yzImgNum").value=''; document.getElementById("yz_Img").src="https://www.cppentry.com/do/yzimg.php?"+Math.random();; } limitTime=parseInt('5'); limitComment(); } //--> </script> </td> <td width="84%"><span class="R"> <input type="button" id="comment_submit" onclick="post_comment()" name="Submit" value="提交" class="button"> <input type="hidden" name="action" value="post"> </span></td> </tr></form> </table> </td> </tr> <tr> <td class="foot"> <h3 class="L"></h3> <h3 class="R"></h3> </td> </tr> </table> </div> <div class="Side"> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="dragTable" id="sonSortName"> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0" > <script>document.write(unescape('%3Cdiv id="hm_t_45532"%3E%3C/div%3E%3Cscript charset="utf-8" src="http://crs.baidu.com/t.js?siteId=238ce0d9669a08cae1971b03e0b2931a&planId=45532&async=0&referer=') + encodeURIComponent(document.referrer) + '&title=' + encodeURIComponent(document.title) + '&rnd=' + (+new Date) + unescape('"%3E%3C/script%3E'));</script> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="dragTable"> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="dragTable"> <script type="text/javascript"> document.write('<a style="display:none!important" id="tanx-a-mm_16790798_2355298_35944706"></a>'); tanx_s = document.createElement("script"); tanx_s.type = "text/javascript"; tanx_s.charset = "gbk"; tanx_s.id = "tanx-s-mm_16790798_2355298_35944706"; tanx_s.async = true; tanx_s.src = "http://p.tanx.com/ex?i=mm_16790798_2355298_35944706"; tanx_h = document.getElementsByTagName("head")[0]; if(tanx_h)tanx_h.insertBefore(tanx_s,tanx_h.firstChild); </script> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="dragTable"> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="dragTable"> </table> </div> </div> <div class="cleardiv"></div> <!-- --> <SCRIPT LANGUAGE="JavaScript"> <!--//目的是为了做风格方便 document.write('</div>'); //--> </SCRIPT> <SCRIPT LANGUAGE="JavaScript"> <!-- clickEdit.init(); //--> </SCRIPT> <div id="copyright"> Copyright@https://www.cppentry.com all rights reserved <a href="http://www.miibeian.gov.cn" target="_blank">粤ICP备13067022号-3</a><br> Powered by <a href="http://www.qibosoft.com" target="_blank">qibosoft V7.0</a> Code © 2003-11 <a href="http://bbs.qibosoft.com/" target="_blank">qibosoft</a> <br> </div> </body> </html>