请选择 进入手机版 | 继续访问电脑版
绿茶科技社区
用正则来匹配dede的标签,对于以后想实现dedecms那样的模板标签的朋友与想研究dedecms模板标签的朋友可以参考下。
html代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title></title>
  6. </head>
  7. <body>
  8. {t:arc row='10'}
  9. {t:field.title/}
  10. {t:list row='5'}
  11. {t:field.title/}
  12. {t:list row='5'}
  13. {t:field.title/}
  14. {/t:list}{t:field.title/}
  15. {/t:list}{t:field.title/}
  16. {/t:arc}
  17. {T:channelartlist
  18. row='6'
  19. }
  20. <dl>
  21. <dt>{t:field name='typename'/}{t:field.typename/}</dt>
  22. <dd>
  23. {t:arclist titlelen='42' row='10'}
  24. <ul class='autod'>
  25. <li>[field:title/]</li>
  26. <li>([field:sendtime function="MyDate('m-d',@me)"/])</li>
  27. </ul>
  28. {/t:arclist}
  29. </dl>{/t:channelartlist
  30. }
  31. {t:global.cfg_title
  32. /}{t:var.cfg_title/}{t:global.cfg_webname /}{t:global.cfg_title/}{t:var.cfg_title/}
  33. {t:field.title function="Html2Text(@me)"/}
  34. {t:include
  35. filename="header.htm"/}
  36. {t:include filename="header.htm"
  37. /}
  38. {t:field.title function='Html2Text(@me)' }{/t:field
  39. }
  40. {t:array name='asdas' }{/t:array
  41. }
  42. {t:channel typeid='1' row='15'}
  43. [field:id/][field:typename/]
  44. {/t:channel
  45. }
  46. {t:channel typeid='1'} [field:id/] [field:typename/]{/t:channel}
  47. {t:channel typeid='1' }[field:id/]
  48. [field:typename/]{/t:channel
  49. }sd
  50. {t:channel}[field:typename/]{/t:channel
  51. }
  52. {t:channel typeid='1' row='20' }[field:userid/]
  53. [field:scores/]
  54. {/t:channel
  55. }
  56. </body>
  57. </html>
复制代码
php代码


  1. <?php
  2. $str = file_get_contents("test.htm");
  3. $skstr="{";
  4. $skdstr="t:";
  5. $sdstr="}";
  6. //$reg="/{t:.*?(?:\/).*?}/si";//此无法匹配嵌套,只能一层 si s: 使前面的. 包括换行符 i:不区分大小写
  7. //$reg="/({t:*[^{]*\/}|{t:*[^}]*}([^{]|(?R))*{\/t:.*?})/si";无限嵌套都可截取.
  8. $reg='/('.$skstr.$skdstr.'*[^'.$skstr.']*\/'.$sdstr.'|'.$skstr.$skdstr.'*[^'.$sdstr.']*'.$sdstr.'([^'.$skstr.']|(?R))*'.$skstr.'\/'.$skdstr.'.*?'.$sdstr.')/si';
  9. preg_match_all($reg,$str,$m);
  10. /*echo "<pre>";print_r($m[0]);echo "</pre>";exit();*/
  11. $CTags=array();
  12. $glarr1=array($skstr.$skdstr=>'');//array('{t:'=>'');前缀
  13. $glarr2=array('"'=>'','\''=>'','输出内容:
  14. [code]
  15. Array
  16. (
  17. [0] => Array
  18. (
  19. [str] => {t:arc row='10'}
  20. {t:field.title/}
  21. {t:list row='5'}
  22. {t:field.title/}
  23. {t:list row='5'}
  24. {t:field.title/}
  25. {/t:list}{t:field.title/}
  26. {/t:list}{t:field.title/}
  27. {/t:arc}
  28. [tagname] => arc
  29. [tagfield] =>
  30. [text] =>
  31. {t:field.title/}
  32. {t:list row='5'}
  33. {t:field.title/}
  34. {t:list row='5'}
  35. {t:field.title/}
  36. {/t:list}{t:field.title/}
  37. {/t:list}{t:field.title/}
  38. [attr] => Array
  39. (
  40. [row] => 10
  41. )
  42. )
  43. [1] => Array
  44. (
  45. [str] => {T:channelartlist
  46. row='6'
  47. }
  48. {t:field name='typename'/}{t:field.typename/}
  49. {t:arclist titlelen='42' row='10'}
  50. [field:title/]
  51. ([field:sendtime function="MyDate('m-d',@me)"/])
  52. {/t:arclist}
  53. {/t:channelartlist
  54. }
  55. [tagname] => channelartlist
  56. [tagfield] =>
  57. [text] =>
  58. {t:field name='typename'/}{t:field.typename/}
  59. {t:arclist titlelen='42' row='10'}
  60. [field:title/]
  61. ([field:sendtime function="MyDate('m-d',@me)"/])
  62. {/t:arclist}
  63. [attr] => Array
  64. (
  65. [row] => 6
  66. )
  67. )
  68. [2] => Array
  69. (
  70. [str] => {t:global.cfg_title
  71. /}
  72. [tagname] => global
  73. [tagfield] => cfg_title
  74. [text] =>
  75. [attr] => Array
  76. (
  77. )
  78. )
  79. [3] => Array
  80. (
  81. [str] => {t:var.cfg_title/}
  82. [tagname] => var
  83. [tagfield] => cfg_title
  84. [text] =>
  85. [attr] => Array
  86. (
  87. )
  88. )
  89. [4] => Array
  90. (
  91. [str] => {t:global.cfg_webname /}
  92. [tagname] => global
  93. [tagfield] => cfg_webname
  94. [text] =>
  95. [attr] => Array
  96. (
  97. )
  98. )
  99. [5] => Array
  100. (
  101. [str] => {t:global.cfg_title/}
  102. [tagname] => global
  103. [tagfield] => cfg_title
  104. [text] =>
  105. [attr] => Array
  106. (
  107. )
  108. )
  109. [6] => Array
  110. (
  111. [str] => {t:var.cfg_title/}
  112. [tagname] => var
  113. [tagfield] => cfg_title
  114. [text] =>
  115. [attr] => Array
  116. (
  117. )
  118. )
  119. [7] => Array
  120. (
  121. [str] => {t:field.title function="Html2Text(@me)"/}
  122. [tagname] => field
  123. [tagfield] => title
  124. [text] =>
  125. [function] => Html2Text(@me)
  126. [attr] => Array
  127. (
  128. )
  129. )
  130. [8] => Array
  131. (
  132. [str] => {t:include
  133. filename="header.htm"/}
  134. [tagname] => include
  135. [tagfield] =>
  136. [text] =>
  137. [attr] => Array
  138. (
  139. [filename] => header.htm
  140. )
  141. )
  142. [9] => Array
  143. (
  144. [str] => {t:include filename="header.htm"
  145. /}
  146. [tagname] => include
  147. [tagfield] =>
  148. [text] =>
  149. [attr] => Array
  150. (
  151. [filename] => header.htm
  152. )
  153. )
  154. [10] => Array
  155. (
  156. [str] => {t:field.title function='Html2Text(@me)' }{/t:field
  157. }
  158. [tagname] => field
  159. [tagfield] => title
  160. [text] =>
  161. [function] => Html2Text(@me)
  162. [attr] => Array
  163. (
  164. )
  165. )
  166. [11] => Array
  167. (
  168. [str] => {t:array name='asdas' }{/t:array
  169. }
  170. [tagname] => array
  171. [tagfield] => asdas
  172. [text] =>
  173. [attr] => Array
  174. (
  175. )
  176. )
  177. [12] => Array
  178. (
  179. [str] => {t:channel typeid='1' row='15'}
  180. [field:id/][field:typename/]
  181. {/t:channel
  182. }
  183. [tagname] => channel
  184. [tagfield] =>
  185. [text] =>
  186. [field:id/][field:typename/]
  187. [attr] => Array
  188. (
  189. [typeid] => 1
  190. [row] => 15
  191. )
  192. )
  193. [13] => Array
  194. (
  195. [str] => {t:channel typeid='1'} [field:id/] [field:typename/]{/t:channel}
  196. [tagname] => channel
  197. [tagfield] =>
  198. [text] => [field:id/] [field:typename/]
  199. [attr] => Array
  200. (
  201. [typeid] => 1
  202. )
  203. )
  204. [14] => Array
  205. (
  206. [str] => {t:channel typeid='1' }[field:id/]
  207. [field:typename/]{/t:channel
  208. }
  209. [tagname] => channel
  210. [tagfield] =>
  211. [text] => [field:id/]
  212. [field:typename/]
  213. [attr] => Array
  214. (
  215. [typeid] => 1
  216. )
  217. )
  218. [15] => Array
  219. (
  220. [str] => {t:channel}[field:typename/]{/t:channel
  221. }
  222. [tagname] => channel
  223. [tagfield] =>
  224. [text] => [field:typename/]
  225. [attr] => Array
  226. (
  227. )
  228. )
  229. [16] => Array
  230. (
  231. [str] => {t:channel typeid='1' row='20' }[field:userid/]
  232. [field:scores/]
  233. {/t:channel
  234. }
  235. [tagname] => channel
  236. [tagfield] =>
  237. [text] => [field:userid/]
  238. [field:scores/]
  239. [attr] => Array
  240. (
  241. [typeid] => 1
  242. [row] => 20
  243. )
  244. )
  245. )
复制代码




=>'','/'=>'');
/*
{t:array typeid='1'}[field:typename/]{/t:array}
{t:global.title function="html2text(@me)"/}
*/
foreach($m[0] as $k=>$v){
$rarr=array();
$rarr['str']=$v; //原模板字符串
$vs=explode('}',$v); //分割取得第一部分 {t:array typeid='1'
$va=explode(' ',$vs[0]); //分割属性 " {t:array " , " typeid='1' "
$vb=explode('.',$va[0]); //分割标签名和字段名 {t:global.title
$rarr['tagname']=trim(strtr(strtolower($vb[0]),$glarr1));//过滤前缀{t: 取得标签名 array 或 golbal
$rarr['tagfield']=strtr($vb[1],$glarr2); //取得字段名 title
unset($vs[0]);$vsstr=implode('}',$vs); //去掉前缀,返回后半段: [field:typename/]{/t:array}
$rarr['text']=preg_replace("/".$skstr."\/".$skdstr.$rarr['tagname']."(.*?)".$sdstr."/si",'',$vsstr);
/*过滤后缀 {/t:array} 取得中间文本.
可能出现,空格或者换行所以使用正则,也可直接使用strtr 替换 。
{/t:array }
{/t:array
}
strtr(strtolower($vsstr),array($skstr.'/'.$skdstr.$rarr['tagname'].$sdstr=>''));
*/
unset($va[0]);
$attr=array();
foreach($va as $val){//循环记录属性
$val=strtr($val,$glarr2); //typeid='1' 去掉 " ' / , typeid=1
$vc=explode('=',$val);
if(!empty($vc[0])){
$vc[0]=strtolower($vc[0]); //小写属性名
if($vc[0]=="function"){
$rarr['function']=$vc[1]; //返回要执行的 html2text(@me)
}else if($vc[0]=="name"){
$rarr['tagfield']=$vc[1]; //{t:field name='title' /}
}else{
$attr[trim($vc[0])]=$vc[1]; //属性 $attr['typeid']=1;
}
}
}
$rarr['attr']=$attr;
$CTags[$k]=$rarr;
}
echo "<pre>";
print_r($CTags);
echo "</pre>";
?> [/code]输出内容:

  1. Array
  2. (
  3. [0] =&gt; Array
  4. (
  5. [str] =&gt; {t:arc row='10'}
  6. {t:field.title/}
  7. {t:list row='5'}
  8. {t:field.title/}
  9. {t:list row='5'}
  10. {t:field.title/}
  11. {/t:list}{t:field.title/}
  12. {/t:list}{t:field.title/}
  13. {/t:arc}
  14. [tagname] =&gt; arc
  15. [tagfield] =&gt;
  16. [text] =&gt;
  17. {t:field.title/}
  18. {t:list row='5'}
  19. {t:field.title/}
  20. {t:list row='5'}
  21. {t:field.title/}
  22. {/t:list}{t:field.title/}
  23. {/t:list}{t:field.title/}
  24. [attr] =&gt; Array
  25. (
  26. [row] =&gt; 10
  27. )
  28. )
  29. [1] =&gt; Array
  30. (
  31. [str] =&gt; {T:channelartlist
  32. row='6'
  33. }
  34. {t:field name='typename'/}{t:field.typename/}
  35. {t:arclist titlelen='42' row='10'}
  36. [field:title/]
  37. ([field:sendtime function="MyDate('m-d',@me)"/])
  38. {/t:arclist}
  39. {/t:channelartlist
  40. }
  41. [tagname] =&gt; channelartlist
  42. [tagfield] =&gt;
  43. [text] =&gt;
  44. {t:field name='typename'/}{t:field.typename/}
  45. {t:arclist titlelen='42' row='10'}
  46. [field:title/]
  47. ([field:sendtime function="MyDate('m-d',@me)"/])
  48. {/t:arclist}
  49. [attr] =&gt; Array
  50. (
  51. [row] =&gt; 6
  52. )
  53. )
  54. [2] =&gt; Array
  55. (
  56. [str] =&gt; {t:global.cfg_title
  57. /}
  58. [tagname] =&gt; global
  59. [tagfield] =&gt; cfg_title
  60. [text] =&gt;
  61. [attr] =&gt; Array
  62. (
  63. )
  64. )
  65. [3] =&gt; Array
  66. (
  67. [str] =&gt; {t:var.cfg_title/}
  68. [tagname] =&gt; var
  69. [tagfield] =&gt; cfg_title
  70. [text] =&gt;
  71. [attr] =&gt; Array
  72. (
  73. )
  74. )
  75. [4] =&gt; Array
  76. (
  77. [str] =&gt; {t:global.cfg_webname /}
  78. [tagname] =&gt; global
  79. [tagfield] =&gt; cfg_webname
  80. [text] =&gt;
  81. [attr] =&gt; Array
  82. (
  83. )
  84. )
  85. [5] =&gt; Array
  86. (
  87. [str] =&gt; {t:global.cfg_title/}
  88. [tagname] =&gt; global
  89. [tagfield] =&gt; cfg_title
  90. [text] =&gt;
  91. [attr] =&gt; Array
  92. (
  93. )
  94. )
  95. [6] =&gt; Array
  96. (
  97. [str] =&gt; {t:var.cfg_title/}
  98. [tagname] =&gt; var
  99. [tagfield] =&gt; cfg_title
  100. [text] =&gt;
  101. [attr] =&gt; Array
  102. (
  103. )
  104. )
  105. [7] =&gt; Array
  106. (
  107. [str] =&gt; {t:field.title function="Html2Text(@me)"/}
  108. [tagname] =&gt; field
  109. [tagfield] =&gt; title
  110. [text] =&gt;
  111. [function] =&gt; Html2Text(@me)
  112. [attr] =&gt; Array
  113. (
  114. )
  115. )
  116. [8] =&gt; Array
  117. (
  118. [str] =&gt; {t:include
  119. filename="header.htm"/}
  120. [tagname] =&gt; include
  121. [tagfield] =&gt;
  122. [text] =&gt;
  123. [attr] =&gt; Array
  124. (
  125. [filename] =&gt; header.htm
  126. )
  127. )
  128. [9] =&gt; Array
  129. (
  130. [str] =&gt; {t:include filename="header.htm"
  131. /}
  132. [tagname] =&gt; include
  133. [tagfield] =&gt;
  134. [text] =&gt;
  135. [attr] =&gt; Array
  136. (
  137. [filename] =&gt; header.htm
  138. )
  139. )
  140. [10] =&gt; Array
  141. (
  142. [str] =&gt; {t:field.title function='Html2Text(@me)' }{/t:field
  143. }
  144. [tagname] =&gt; field
  145. [tagfield] =&gt; title
  146. [text] =&gt;
  147. [function] =&gt; Html2Text(@me)
  148. [attr] =&gt; Array
  149. (
  150. )
  151. )
  152. [11] =&gt; Array
  153. (
  154. [str] =&gt; {t:array name='asdas' }{/t:array
  155. }
  156. [tagname] =&gt; array
  157. [tagfield] =&gt; asdas
  158. [text] =&gt;
  159. [attr] =&gt; Array
  160. (
  161. )
  162. )
  163. [12] =&gt; Array
  164. (
  165. [str] =&gt; {t:channel typeid='1' row='15'}
  166. [field:id/][field:typename/]
  167. {/t:channel
  168. }
  169. [tagname] =&gt; channel
  170. [tagfield] =&gt;
  171. [text] =&gt;
  172. [field:id/][field:typename/]
  173. [attr] =&gt; Array
  174. (
  175. [typeid] =&gt; 1
  176. [row] =&gt; 15
  177. )
  178. )
  179. [13] =&gt; Array
  180. (
  181. [str] =&gt; {t:channel typeid='1'} [field:id/] [field:typename/]{/t:channel}
  182. [tagname] =&gt; channel
  183. [tagfield] =&gt;
  184. [text] =&gt; [field:id/] [field:typename/]
  185. [attr] =&gt; Array
  186. (
  187. [typeid] =&gt; 1
  188. )
  189. )
  190. [14] =&gt; Array
  191. (
  192. [str] =&gt; {t:channel typeid='1' }[field:id/]
  193. [field:typename/]{/t:channel
  194. }
  195. [tagname] =&gt; channel
  196. [tagfield] =&gt;
  197. [text] =&gt; [field:id/]
  198. [field:typename/]
  199. [attr] =&gt; Array
  200. (
  201. [typeid] =&gt; 1
  202. )
  203. )
  204. [15] =&gt; Array
  205. (
  206. [str] =&gt; {t:channel}[field:typename/]{/t:channel
  207. }
  208. [tagname] =&gt; channel
  209. [tagfield] =&gt;
  210. [text] =&gt; [field:typename/]
  211. [attr] =&gt; Array
  212. (
  213. )
  214. )
  215. [16] =&gt; Array
  216. (
  217. [str] =&gt; {t:channel typeid='1' row='20' }[field:userid/]
  218. [field:scores/]
  219. {/t:channel
  220. }
  221. [tagname] =&gt; channel
  222. [tagfield] =&gt;
  223. [text] =&gt; [field:userid/]
  224. [field:scores/]
  225. [attr] =&gt; Array
  226. (
  227. [typeid] =&gt; 1
  228. [row] =&gt; 20
  229. )
  230. )
  231. )
复制代码




分享到 :
0 人收藏

3 个回复

倒序浏览
Teddys Knowledg  高级会员 | 2019-9-26 02:07:00
啥玩应呀
北岛函  高级会员 | 2019-9-27 09:54:50
鼎力支持!!
JustRun  金牌会员 | 2019-10-16 07:15:41
众里寻他千百度,蓦然回首在这里!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

举报|Archiver|手机版|小黑屋|Lvchakeji Inc.  

Powered by Discuz! X3.3 © 2001-2016 Comsenz Inc.

返回顶部