首页  >  文章  >  后端开发  >  php下正则来匹配dede模板标签的代码_php技巧

php下正则来匹配dede模板标签的代码_php技巧

WBOY
WBOY原创
2016-05-17 09:23:251091浏览

html代码

复制代码 代码如下:



<头>
>
<标题>

<身体>
{t:arc row='10'}
{t:field.title/}
{t:list row='5'}
{t:field.title/}
{t:list row='5'}
{t:field.title/}
{/t:list}{t:field.title/}
{/t:list}{t :field.title/}
{/t:arc}
{T:channelartlist
row='6'
}
;
{t:field name='typename'/}{t:field.typename/}
;

{t:arclist titlelen='42' row='10'}
    ;
  • [字段:标题/]

  • ([field:sendtime function="MyDate('m-d',@me)"/])
  • ;

{/t:arclist}
{/t:channelartlist
}
{t:global.cfg_title
/}{t:var.cfg_title/}{t :global.cfg_webname /}{t:global.cfg_title/}{t:var.cfg_title/}
{t:field.title function="Html2Text(@me)"/}
{t:include
filename="header.htm"/}
{t:include filename="header.htm"
/}
{t:field.title function='Html2Text(@me)' }{ /t:field
}
{t:array name='asdas' }{/t:array
}
{t:channel typeid='1' row='15'}
[field:id/][field:typename/]
{/t:channel
}
{t:channel typeid='1'} [field:id/] [field:typename/] {/t:channel}
{t:channel typeid='1' }[field:id/]
[field:typename/]{/t:channel
}sd
{t:频道}[field:typename/]{/t:channel
}
{t:channel typeid='1' row='20' }[field:userid/]
[field:scores/]
{/t:频道
}



php代码
复制代码代码如下:

< ;?php
$str = file_get_contents("test.htm");
$skstr="{";
$skdstr="t:";
$sdstr="}";
//$reg="/{t:.*?(?:/).*?}/si";//此无法匹配乔丹,只能一层 si s: 使前面的。 包括换行符 i:不区分大小写
//$reg="/({t:*[^{]*/}|{t:*[^}]*}([^{]|(? R))*{/t:.*?})/si";无限无限都可截取。
$reg='/('.$skstr.$skdstr.'*[^'.$skstr.']*/'.$sdstr.'|'.$skstr.$skdstr.'*[^' .$sdstr.']*'.$sdstr.'([^'.$skstr.']|(?R))*'.$skstr.'/'.$skdstr.'.*?'.$sdstr .')/si';
preg_match_all($reg,$str,$m);
/*echo "
";print_r($m[0]);echo "
";exit();*/
$CTags=array();
$glarr1=array($skstr.$skdstr=>'');//array('{t:'=>'');外部
$glarr2=array('"'=> ;'','''=>'','$'=>'','/'=>'');
/*
{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: 获取标签名数组 或 golbal
$rarr['tagfield']=strtr($vb[1],$glarr2) ; // 获取字段名标题
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=>''));
*/
未设置($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 "
" <br>print_r($CTags) <br>echo "
";

;
输出内容:
复制代码 代码如下:

数组
(
[0] => 数组
(
[str] => {t:arc row='10'}
{t: field.title/}
{t:list row='5'}
{t:field.title/}
{t:list row='5'}
{t:field.标题/}
{/t:list}{t:field.title/}
{/t:list}{t:field.title/}
{/t:arc}
[标记名] => arc
[标签字段]
[文本] =>
{t:field.title/}
{t:list row='5'} {t:field.title/}
{t:list row='5'}
{t:field.title/}
{/t:list}{t:field.title/}
{/t:list}{t:field.title/}
[attr] => 数组
(
[row] => 10
)
)
[1] => 数组
(
[str] => {T:channelartlist
row='6'
}
{t:字段名称='typename' /}{t:field.typename/}
{t:arclist titlelen='42' row='10'}
[field:title/]
([field:sendtime function="MyDate( 'm-d',@me)"/])
{/t:arclist}
{/t:channelartlist
}
[标签名称] =>channelartlist
[标签字段] =>
[文本] =>
{t:字段名称='typename'/}{t:field.typename/}
{t:arclist titlelen='42' row='10 '}
[field:title/]
([field:sendtime function="MyDate('m-d',@me)"/])
{/t:arclist}
[属性] =>数组
(
[行] => 6
)
)
[2] =>数组
(
[str] => {t:global.cfg_title
/}
[标签名] => 全局
[标签字段] => cfg_title
[文本] =>
[attr] => 数组
(
)
)
[3] =>;数组
(
[str] => {t:var.cfg_title/}
[tagname] => var
[tagfield] => cfg_title
[text] => ;
[attr] => 数组
(
)
)
[4] =>;数组
(
[str] => {t:global.cfg_webname /}
[tagname] => 全局
[tagfield] => cfg_webname
[text] => ;
[attr] => 数组
(
)
)
[5] =>;数组
(
[str] => {t:global.cfg_title/}
[tagname] => 全局
[tagfield] => cfg_title
[text] => ;
[attr] => 数组
(
)
)
[6] =>;数组
(
[str] => {t:var.cfg_title/}
[tagname] => var
[tagfield] => cfg_title
[text] => ;
[attr] => 数组
(
)
)
[7] =>;数组
(
[str] => {t:field.title function="Html2Text(@me)"/}
[tagname] => 字段
[tagfield] =>;标题
[文本] =>
[函数] => Html2Text(@me)
[attr] => 数组
(
)
)
[ 8] =>数组
(
[str] => {t:include
filename="header.htm"/}
[tagname] => include
[tagfield] =>
[文本] =>
[attr] => 数组
(
[文件名] => header.htm
)
)
[9] => ;数组
(
[str] => {t:include filename="header.htm"
/}
[tagname] => include
[tagfield] =>
[文本] =>
[attr] => 数组
(
[文件名] => header.htm
)
)
[10] => ;数组
(
[str] => {t:field.title function='Html2Text(@me)' }{/t:field
}
[标签名] => field
[标签字段] => 标题
[文本] =>
[函数] => Html2Text(@me)
[attr] => 数组
(
)
)
[11] =>数组
(
[str] => {t:array name='asdas' }{/t:array
}
[标签名称] => 数组
[tagfield] = > asdas
[text] =>
[attr] => 数组
(
)
)
[12] =>数组
(
[str] => {t:channel typeid='1' row='15'}
[field:id/][field:typename/]
{/t :频道
}
[标签名称] => 频道
[标签字段] =>
[文本] =>
[字段:id/][字段:类型名称/]
[attr] => 数组
(
[typeid] => 1
[row] => 15
)
)
[13] =>数组
(
[str] => {t:channel typeid='1'} [field:id/] [field:typename/]{/t:channel}
[tagname] => ; 频道
[标签字段] =>
[文本] => [字段:id/] [字段:类型名称/]
[attr] => 数组
(
[类型id] => 1
)
)
[14] =>数组
(
[str] => {t:channel typeid='1' }[field:id/]
[field:typename/]{/t:channel
}
[标签名称] => 频道
[标签字段] =>
[文本] => [字段:id/]
[字段:类型名称/]
[attr] =>数组
(
[typeid] => 1
)
)
[15] =>数组
(
[str] => {t:channel}[field:typename/]{/t:channel
}
[标签名] => 频道
[tagfield] => [文本] => [字段:类型名称/]
[属性] => 数组
(
)
)
[16] =>数组
(
[str] => {t:channel typeid='1' row='20' }[field:userid/]
[field:scores/]
{/t :频道
}
[tagname] => channel
[tagfield] =>
[text] => [field:userid/]
[field:scores/]
[attr] => Array
(
[typeid] => 1
[row] => 20
)
)
)

测试代码打包下载
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn