php下正则来匹配dede模板标签的代码
程序员文章站
2022-10-06 18:17:14
html代码 复制代码 代码如下:
html代码
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
{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'
}
<dl>
<dt>{t:field name='typename'/}{t:field.typename/}</dt>
<dd>
{t:arclist titlelen='42' row='10'}
<ul class='autod'>
<li>[field:title/]</li>
<li>([field:sendtime function="mydate('m-d',@me)"/])</li>
</ul>
{/t:arclist}
</dl>{/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:channel}[field:typename/]{/t:channel
}
{t:channel typeid='1' row='20' }[field:userid/]
[field:scores/]
{/t:channel
}
</body>
</html>
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 "<pre>";print_r($m[0]);echo "</pre>";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: 取得标签名 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>";
?>
输出内容:
array
(
[0] => array
(
[str] => {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}
[tagname] => arc
[tagfield] =>
[text] =>
{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] => array
(
[row] => 10
)
)
[1] => array
(
[str] => {t:channelartlist
row='6'
}
{t:field name='typename'/}{t:field.typename/}
{t:arclist titlelen='42' row='10'}
[field:title/]
([field:sendtime function="mydate('m-d',@me)"/])
{/t:arclist}
{/t:channelartlist
}
[tagname] => channelartlist
[tagfield] =>
[text] =>
{t:field name='typename'/}{t:field.typename/}
{t:arclist titlelen='42' row='10'}
[field:title/]
([field:sendtime function="mydate('m-d',@me)"/])
{/t:arclist}
[attr] => array
(
[row] => 6
)
)
[2] => array
(
[str] => {t:global.cfg_title
/}
[tagname] => global
[tagfield] => cfg_title
[text] =>
[attr] => array
(
)
)
[3] => array
(
[str] => {t:var.cfg_title/}
[tagname] => var
[tagfield] => cfg_title
[text] =>
[attr] => array
(
)
)
[4] => array
(
[str] => {t:global.cfg_webname /}
[tagname] => global
[tagfield] => cfg_webname
[text] =>
[attr] => array
(
)
)
[5] => array
(
[str] => {t:global.cfg_title/}
[tagname] => global
[tagfield] => cfg_title
[text] =>
[attr] => array
(
)
)
[6] => array
(
[str] => {t:var.cfg_title/}
[tagname] => var
[tagfield] => cfg_title
[text] =>
[attr] => array
(
)
)
[7] => array
(
[str] => {t:field.title function="html2text(@me)"/}
[tagname] => field
[tagfield] => title
[text] =>
[function] => html2text(@me)
[attr] => array
(
)
)
[8] => array
(
[str] => {t:include
filename="header.htm"/}
[tagname] => include
[tagfield] =>
[text] =>
[attr] => array
(
[filename] => header.htm
)
)
[9] => array
(
[str] => {t:include filename="header.htm"
/}
[tagname] => include
[tagfield] =>
[text] =>
[attr] => array
(
[filename] => header.htm
)
)
[10] => array
(
[str] => {t:field.title function='html2text(@me)' }{/t:field
}
[tagname] => field
[tagfield] => title
[text] =>
[function] => html2text(@me)
[attr] => array
(
)
)
[11] => array
(
[str] => {t:array name='asdas' }{/t:array
}
[tagname] => array
[tagfield] => asdas
[text] =>
[attr] => array
(
)
)
[12] => array
(
[str] => {t:channel typeid='1' row='15'}
[field:id/][field:typename/]
{/t:channel
}
[tagname] => channel
[tagfield] =>
[text] =>
[field:id/][field:typename/]
[attr] => array
(
[typeid] => 1
[row] => 15
)
)
[13] => array
(
[str] => {t:channel typeid='1'} [field:id/] [field:typename/]{/t:channel}
[tagname] => channel
[tagfield] =>
[text] => [field:id/] [field:typename/]
[attr] => array
(
[typeid] => 1
)
)
[14] => array
(
[str] => {t:channel typeid='1' }[field:id/]
[field:typename/]{/t:channel
}
[tagname] => channel
[tagfield] =>
[text] => [field:id/]
[field:typename/]
[attr] => array
(
[typeid] => 1
)
)
[15] => array
(
[str] => {t:channel}[field:typename/]{/t:channel
}
[tagname] => channel
[tagfield] =>
[text] => [field:typename/]
[attr] => array
(
)
)
[16] => array
(
[str] => {t:channel typeid='1' row='20' }[field:userid/]
[field:scores/]
{/t:channel
}
[tagname] => channel
[tagfield] =>
[text] => [field:userid/]
[field:scores/]
[attr] => array
(
[typeid] => 1
[row] => 20
)
)
)
复制代码 代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
{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'
}
<dl>
<dt>{t:field name='typename'/}{t:field.typename/}</dt>
<dd>
{t:arclist titlelen='42' row='10'}
<ul class='autod'>
<li>[field:title/]</li>
<li>([field:sendtime function="mydate('m-d',@me)"/])</li>
</ul>
{/t:arclist}
</dl>{/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:channel}[field:typename/]{/t:channel
}
{t:channel typeid='1' row='20' }[field:userid/]
[field:scores/]
{/t:channel
}
</body>
</html>
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 "<pre>";print_r($m[0]);echo "</pre>";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: 取得标签名 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>";
?>
输出内容:
复制代码 代码如下:
array
(
[0] => array
(
[str] => {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}
[tagname] => arc
[tagfield] =>
[text] =>
{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] => array
(
[row] => 10
)
)
[1] => array
(
[str] => {t:channelartlist
row='6'
}
{t:field name='typename'/}{t:field.typename/}
{t:arclist titlelen='42' row='10'}
[field:title/]
([field:sendtime function="mydate('m-d',@me)"/])
{/t:arclist}
{/t:channelartlist
}
[tagname] => channelartlist
[tagfield] =>
[text] =>
{t:field name='typename'/}{t:field.typename/}
{t:arclist titlelen='42' row='10'}
[field:title/]
([field:sendtime function="mydate('m-d',@me)"/])
{/t:arclist}
[attr] => array
(
[row] => 6
)
)
[2] => array
(
[str] => {t:global.cfg_title
/}
[tagname] => global
[tagfield] => cfg_title
[text] =>
[attr] => array
(
)
)
[3] => array
(
[str] => {t:var.cfg_title/}
[tagname] => var
[tagfield] => cfg_title
[text] =>
[attr] => array
(
)
)
[4] => array
(
[str] => {t:global.cfg_webname /}
[tagname] => global
[tagfield] => cfg_webname
[text] =>
[attr] => array
(
)
)
[5] => array
(
[str] => {t:global.cfg_title/}
[tagname] => global
[tagfield] => cfg_title
[text] =>
[attr] => array
(
)
)
[6] => array
(
[str] => {t:var.cfg_title/}
[tagname] => var
[tagfield] => cfg_title
[text] =>
[attr] => array
(
)
)
[7] => array
(
[str] => {t:field.title function="html2text(@me)"/}
[tagname] => field
[tagfield] => title
[text] =>
[function] => html2text(@me)
[attr] => array
(
)
)
[8] => array
(
[str] => {t:include
filename="header.htm"/}
[tagname] => include
[tagfield] =>
[text] =>
[attr] => array
(
[filename] => header.htm
)
)
[9] => array
(
[str] => {t:include filename="header.htm"
/}
[tagname] => include
[tagfield] =>
[text] =>
[attr] => array
(
[filename] => header.htm
)
)
[10] => array
(
[str] => {t:field.title function='html2text(@me)' }{/t:field
}
[tagname] => field
[tagfield] => title
[text] =>
[function] => html2text(@me)
[attr] => array
(
)
)
[11] => array
(
[str] => {t:array name='asdas' }{/t:array
}
[tagname] => array
[tagfield] => asdas
[text] =>
[attr] => array
(
)
)
[12] => array
(
[str] => {t:channel typeid='1' row='15'}
[field:id/][field:typename/]
{/t:channel
}
[tagname] => channel
[tagfield] =>
[text] =>
[field:id/][field:typename/]
[attr] => array
(
[typeid] => 1
[row] => 15
)
)
[13] => array
(
[str] => {t:channel typeid='1'} [field:id/] [field:typename/]{/t:channel}
[tagname] => channel
[tagfield] =>
[text] => [field:id/] [field:typename/]
[attr] => array
(
[typeid] => 1
)
)
[14] => array
(
[str] => {t:channel typeid='1' }[field:id/]
[field:typename/]{/t:channel
}
[tagname] => channel
[tagfield] =>
[text] => [field:id/]
[field:typename/]
[attr] => array
(
[typeid] => 1
)
)
[15] => array
(
[str] => {t:channel}[field:typename/]{/t:channel
}
[tagname] => channel
[tagfield] =>
[text] => [field:typename/]
[attr] => array
(
)
)
[16] => array
(
[str] => {t:channel typeid='1' row='20' }[field:userid/]
[field:scores/]
{/t:channel
}
[tagname] => channel
[tagfield] =>
[text] => [field:userid/]
[field:scores/]
[attr] => array
(
[typeid] => 1
[row] => 20
)
)
)